编写文本
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>canvas编写文本</title>
<style type="text/css">
canvas {
border: 1px solid black;
margin: 50px auto;
}
</style>
</head>
<body>
<!-- canvas有兼容性问题,标签不识别时会读取里面内容 -->
<canvas
id="myCanvas"
width="500"
height="500"
style="border:1px solid #000000;"
>对不起,你的浏览器版本过低,点击<a href="#">下载</a>
</canvas>
</body>
<script>
window.onload = function() {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.font = "30px Arial";
ctx.strokeText("年少的欢喜是你", 50, 100);
ctx.fillStyle = "dimgray";
ctx.fillText("你是喜欢的少年", 50, 230);
};
</script>
</html>
绘制图片
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>canvas图片</title>
<style type="text/css">
canvas {
}
</style>
</head>
<body>
<!-- canvas有兼容性问题,标签不识别时会读取里面内容 -->
<canvas id="myCanvas" width="500" height="500">对不起,你的浏览器版本过低,点击<a href="#">下载</a></canvas>
<img
style="display:none;"
src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1547028784732&di=77ff5804bf424d6d0cb0c175b2ff7f91&imgtype=0&src=http%3A%2F%2Fi2.hdslb.com%2Fbfs%2Fface%2F0f27137c1993f1a4034bb83349fb65601b7b420e.jpg"
alt=""
/>
<img style="display:none;" src="./images/timg.png" alt="" />
</body>
<script>
window.onload = function() {
var imgUrl = document.getElementsByTagName("img")[0].src;
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.drawImage(document.getElementsByTagName("img")[1], 0, 0,500,500);
ctx.drawImage(document.getElementsByTagName("img")[0], 130, 150, 250, 250);
};
</script>
</html>
小程序组件调用
ready() {
this.ctx = wx.createCanvasContext('myCanvas', this)
this.drawDetailsImg();
},
小程序组件中保存canvas绘制图片
saveImg() {
this.drawDetailsImg();
wx.showLoading({
title: '加载中',
})
this.ctx.draw(false, wx.canvasToTempFilePath({
canvasId: 'myCanvas',
success(res) {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success: () => {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: () => {
wx.hideLoading();
wx.showToast({
title: '图片保存成功'
})
}
})
}
})
},
fail: function (res) {
console.log(res);
}
},this));
}
canvas特殊方法讲解
drawNormalImg(){
var that = this;
let normal_img = that.data.normal_img;
wx.getImageInfo({
src: normal_img,
success: res => {
that.ctx.drawImage(res.path, 0, 0, that.data.width, that.data.height);
that.ctx.save();
that.ctx.beginPath();
that.ctx.arc(164, 1502, 90, 0, 2 * Math.PI);
that.ctx.fillStyle = "#fff";
that.ctx.fill();
that.ctx.save();
that.ctx.beginPath();
let image = that.data.avatarUrl;
wx.getImageInfo({
src: image,
success: res => {
that.ctx.arc(164, 1502, 81, 0, 2 * Math.PI);
that.ctx.clip();
let tempPath = res.path;
that.ctx.drawImage(tempPath, 74, 1412, 180, 180);
that.ctx.restore();
that.ctx.setFontSize(40);
that.ctx.setFillStyle('#999999');
that.ctx.fillText('用户昵称有十个字的啊:', 84, 1648);
that.ctx.save();
that.ctx.setFillStyle('#333');
that.ctx.font = "normal bold 58px 'Microsoft YaHei'";
that.ctx.fillText('买农资 上大丰收', 84, 1726);
that.ctx.save();
that.ctx.beginPath();
let img = that.data.ewmUrl;
wx.getImageInfo({
src: img,
success: res => {
that.ctx.drawImage(res.path, 708, 1547, 292, 292);
that.ctx.restore();
that.ctx.draw();
}
});
}
})
}
});
},
drawDetailsImg(){
var that = this;
var details_img = that.data.details_img;
wx.getImageInfo({
src: details_img,
success: res => {
that.ctx.drawImage(res.path, 0, 0, that.data.width, that.data.height);
that.ctx.save();
that.ctx.arc(135, 454, 89, 0, 2 * Math.PI);
that.ctx.fillStyle = "#fff";
that.ctx.fill();
that.ctx.save();
that.ctx.beginPath();
let image = that.data.avatarUrl;
wx.getImageInfo({
src: image,
success: res => {
that.ctx.arc(135, 454, 81, 0, 2 * Math.PI);
that.ctx.clip();
let tempPath = res.path;
that.ctx.drawImage(tempPath, 45, 364, 178, 178);
that.ctx.restore();
that.ctx.setFontSize(40);
that.ctx.setFillStyle('#999999');
that.ctx.fillText('用户昵称有十个字的啊:', 251, 424);
that.ctx.save();
that.ctx.setFillStyle('#333');
that.ctx.font = "normal bold 54px 'Microsoft YaHei'";
that.ctx.fillText('买农资 上大丰收', 251, 504);
that.ctx.save();
that.ctx.beginPath();
let pro_img = that.data.proUrl;
wx.getImageInfo({
src: pro_img,
success: res => {
that.ctx.drawImage(res.path, 90, 562, 899, 899);
that.ctx.restore();
that.ctx.setFillStyle('#FF5146');
that.ctx.font = "normal bold 54px 'Microsoft YaHei'";
that.ctx.fillText('¥', 53, 1559);
that.ctx.setFillStyle('#FF5146');
that.ctx.font = "normal bold 81px 'Microsoft YaHei'";
that.ctx.fillText('549', 104, 1559);
that.ctx.save();
that.ctx.setFillStyle('#999999');
that.ctx.font = "normal normal 45px 'Microsoft YaHei'";
that.ctx.fillText('¥999', 53, 1617);
var width = Math.floor(that.ctx.measureText('¥999').width);
that.ctx.beginPath();
that.ctx.moveTo(53, 1600);
that.ctx.lineTo(60 + width, 1600);
that.ctx.lineWidth = 2;
that.ctx.strokeStyle = "#999999";
that.ctx.stroke();
that.ctx.save();
that.ctx.setFillStyle('#333');
that.ctx.font = "normal bold 54px 'Microsoft YaHei'";
var strArr = that.strSplit(that.data.pro_text, 550);
that.ctx.fillText(strArr[0], 53, 1700);
that.ctx.fillText(strArr[1], 53, 1766);
that.ctx.save();
that.ctx.beginPath();
let img = that.data.ewmUrl;
wx.getImageInfo({
src: img,
success: res => {
that.ctx.drawImage(res.path, 708, 1547, 292, 292);
that.ctx.restore();
that.ctx.draw();
}
});
}
});
}
})
}
});
},
strSplit(str,w){
var temp = '';
var row = [];
for (var i = 0; i < str.split('').length; i++) {
if (this.ctx.measureText(temp).width < w) {
} else {
row.push(temp);
temp = "";
}
temp += str.split('')[i];
}
row.push(temp);
return row;
},
canvas绘制图片时获取定宽图片高
img.onload = () => {
const ratio = img.height / img.width
const halfHeight = ratio * halfWidth
}