canvas学习笔记
画基本图形
1.矩形
const canvas = document.getElementById('canvas');
if (canvas.getContext) {
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'rgb(200,0,0)';
ctx.fillRect(0, 0, 300, 50);
ctx.fillStyle = 'rgb(0,200,0)';
ctx.strokeRect(0, 0, 300, 80);
ctx.fillStyle = 'rgb(0,0,200)';
ctx.clearRect(0, 70, 300, 30);
}
2. 三角形
const canvas = document.getElementById('canvas');
if (canvas.getContext) {
const ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.moveTo(25, 25); 起始点
ctx.lineTo(50, 50);
ctx.lineTo(25, 50);
ctx.fill();
ctx.beginPath();
ctx.moveTo(50, 50);
ctx.lineTo(75, 50);
ctx.lineTo(75, 25);
ctx.closePath();
ctx.stroke();
}
3. 画圆
const canvas = document.getElementById('canvas');
if (canvas.getContext) {
const ctx = canvas.getContext('2d');
ctx.strokeStyle = 'Orange';
ctx.lineWidth = 10;
ctx.arc(400, 300, 150, 0, Math.PI*2, true);
ctx.stroke();
}
文字
const canvas = document.getElementById('canvas');
if (canvas.getContext) {
const ctx = canvas.getContext('2d');
var lingrad = ctx.createLinearGradient(100,200,500,200);
lingrad.addColorStop(0.5, '#cc6677')
lingrad.addColorStop(1, '#000')
ctx.shadowColor = 'Orange'
ctx.shadowBlur = 10;
ctx.shadowOffsetX = 20;
ctx.shadowOffsetY = 20;
ctx.font = "bold italic 160px arial";
ctx.fillStyle = lingrad;
ctx.fillText("Hello", 100, 200);
画图
1.原始方法
const canvas = document.getElementById('canvas');
if (canvas.getContext) {
const ctx = canvas.getContext('2d');
const img = new Image();
img.src = "路径.jpg";
img.onload = function() {
const ptrn = ctx.createPattern(img, 'repeat');
ctx.fillStyle = ptrn;
ctx.fillRect(0,0, 800, 600);
}
}
2.常用方法
const canvas = document.getElementById('canvas');
if (canvas.getContext) {
const ctx = canvas.getContext('2d');
const img = new Image();
img.src = "路径.jpg";
img.onload = function() {
ctx.drawImage(this, 0,0, 120, 100, 150, 50, 100, 50);
}
3. 图片上写字
const canvas = document.getElementById('canvas');
if (canvas.getContext) {
const ctx = canvas.getContext('2d');
const img = new Image();
img.src = "路径.jpg";
img.onload = function() {
ctx.drawImage(this, 0, 0);
ctx.strokeStyle = 'rgb(0,0,0)';
ctx.font='10 Arial';
ctx.strokeText('Test Text', 176, 120);
ctx.stroke();
}
}
4.切圆 (Avatar)
const canvas = document.getElementById('canvas');
if (canvas.getContext) {
const ctx = canvas.getContext('2d');
const img = new Image();
img.src = "路径.jpg";
img.onload = function() {
ctx.beginPath();
ctx.arc(400, 300, 150, 0, Math.PI*2);
ctx.fill();
ctx.clip();
ctx.drawImage(this, 80, 100);
}
事件
1.画图板
const canvas = document.getElementById('canvas');
if (canvas.getContext) {
const ctx = canvas.getContext('2d');
canvas.onmousedown = function (e) {
var ev = e || window.event;
var x = ev.clientX - canvas.offsetLeft;
var y = ev.clientY - canvas.offsetTop;
ctx.strokeStyle = 'green';
ctx.lineWidth = 10;
ctx.beginPath();
ctx.moveTo(x, y);
canvas.onmousemove = function (e) {
var ev = e || window.event;
var x = ev.clientX - canvas.offsetLeft;
var y = ev.clientY - ev.offsetTop;
ctx.lineTo(x, y);
ctx.stroke();
}
canvas.onmouseup = function () {
canvas.onmousemove = "";
}
}
2. 刮刮卡
const canvas = document.getElementById('canvas');
if (canvas.getContext) {
const ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.fillStyle = 'gray';
ctx. fillRect(0, 0, canvas.width, canvas.height);
ctx.globalCompositeOperation = 'destination-out';
ctx.lineWidth = 20;
ctx.lineCap = 'round';
canvas.onmousedown = function (e) {
var ev = e || window.event;
var x = ev.clientX - canvas.offsetLeft;
var y = ev.clientY - canvas.offsetTop;
ctx.moveTo(x, y);
canvas.onmousemove = function (e) {
var ev = e || window.event;
var x = ev.clientX;
var y = ev.clientY;
ctx.lineTo(x, y);
ctx.stroke();
};
canvas.onmouseup = function () {
canvas.onmousemove = "";
}
}