<view :style="'background-image:url(' + srcUrl + ');background-size: ' + canvasWidth + 'px 80px;'"></view>
<canvas canvas-id="firstCanvas" :style="'width: ' + canvasWidth + 'px; height: 80px;visibility: hidden;position: absolute;top:-500px'"></canvas>
onLoad() {
getBackImage().then(function(data){
that.canvasWidth = data.width;
that.srcUrl = data.url;
})
}
getBackImage() {
let text1 = "文字一";
let time = new Date;
let text2 = (time.getMonth() + 1) + "" + time.getDate() + "" + time.getHours() + "" + time.getMinutes();
let width = text1.length > text2.length ? (text1.length * 12 + 40) : (text2.length * 12 + 40);
let height = 80;
var ctx = uni.createCanvasContext("firstCanvas");
ctx.setFontSize(12);
ctx.rotate((-20 * Math.PI) / 180)
ctx.setFillStyle("rgba(192, 192, 192, 0.3)");
ctx.setTextBaseline('middle');
ctx.setTextAlign('left');
ctx.fillText(text1, 0, height);
ctx.fillText(text2, 0, height - 30);
return new Promise(function(resolve, reject){
ctx.draw(false,()=>{
uni.canvasToTempFilePath({
x: 0,
y: 0,
width: width,
height: height,
destWidth: width * 4,
destHeight: height * 4,
canvasId: "firstCanvas",
quality: 1,
success: function(res) {
let obj = {
width: width,
url: res.tempFilePath
}
resolve(obj);
}
})
});
});
}