@ArrowLLL
2018-08-07T11:07:07.000000Z
字数 22477
阅读 1231
canvas
Canvas绘图详解 课程笔记
基于状态的绘制
<canvas id="canvas" style="margin: 50px auto;">
not support
</canvas>
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = 800;
canvas.height = 800;
var context = canvas.getContext("2d");
context.moveTo(100, 100);
context.lineTo(700, 700);
context.lineWidth = 10;
context.strokeStyle = "#058";
context.stroke();
}
beginPath() 的使用
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = 800;
canvas.height = 800;
var context = canvas.getContext("2d");
context.lineWidth = 10;
context.beginPath();
context.moveTo(100, 200);
context.lineTo(300, 400);
context.lineTo(100, 600);
context.strokeStyle = "red";
context.stroke();
context.beginPath();
context.moveTo(300, 200);
context.lineTo(500, 400);
context.lineTo(300, 600);
context.strokeStyle = "green";
context.stroke();
context.beginPath();
context.moveTo(500, 200);
context.lineTo(700, 400);
context.lineTo(500, 600);
context.strokeStyle = "blue";
context.stroke();
}
window.onload = function() {
var canvas = document.getElementById('canvas');
var context = canvas.getContext("2d");
canvas.width = canvas.height = 800;
context.beginPath();
context.moveTo(100, 350);
context.lineTo(500, 350);
context.lineTo(500, 200);
context.lineTo(700, 400);
context.lineTo(500, 600);
context.lineTo(500, 450);
context.lineTo(100, 450);
context.closePath();
context.lineWidth=10;
context.fillStyle='yellow';
context.strokeStyle='#058';
context.fill();
context.stroke();
}
ctx.rect(x, y, width, height);
function drawRect(ctx, x, y, width, height, borderWidth, borderColor, fillColor) {
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x + width, y);
ctx.lineTo(x + width, y + height);
ctx.lineTo(x, y + height);
ctx.closePath();
ctx.fillStyle = fillColor;
ctx.strokeStyle = borderColor;
ctx.lineWidth = borderWidth;
ctx.fill();
ctx.stroke();
}
function drawRect2(ctx, x, y, width, height, borderWidth, borderColor, fillColor) {
ctx.lineWidth = borderWidth;
ctx.fillStyle = fillColor;
ctx.strokeStyle = borderColor;
ctx.fillRect(x, y, width, height);
ctx.strokeRect(x, y, width, height);
}
window.onload = function() {
var canvas = document.getElementById('canvas');
var context = canvas.getContext("2d");
canvas.width = canvas.height = 800;
drawRect(context, 100, 100, 400, 400, 10, "#058", "red");
drawRect2(context, 300, 300, 400, 400, 10, "#058", "rgba(0, 255, 0, 0.5)");
}
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext("2d");
context.lineWidth = 30;
context.strokeStyle='#058';
context.beginPath();
context.moveTo(100, 200);
context.lineTo(700, 200);
context.lineCap = 'butt';
context.stroke();
context.beginPath();
context.moveTo(100, 400);
context.lineTo(700, 400);
context.lineCap='round';
context.stroke();
context.beginPath();
context.moveTo(100, 600);
context.lineTo(700, 600);
context.lineCap = 'square';
context.stroke();
//baseline
context.lineWidth = 1;
context.strokeStyle = '#27a';
context.moveTo(100, 100);
context.lineTo(100, 700);
context.moveTo(700, 100);
context.lineTo(700, 700);
context.stroke();
}
function drawStar(ctx, r, R, x, y, rot=0) {
var drawStarLine = (angle, radius) => {
ctx.lineTo(Math.cos(angle) * radius + x, -Math.sin(angle) * radius + y);
}
ctx.beginPath();
for(var i = 0; i < 5; i++) {
var ang = (18 + i * 72 - rot) / 180 * Math.PI;
drawStarLine(ang, R);
ang = (54 + i * 72 - rot) / 180 * Math.PI;
drawStarLine(ang, r);
}
ctx.closePath();
ctx.stroke()
}
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext("2d");
context.lineWidth=10;
drawStar(context, 150, 300, 400, 400);
}
context.lineJoin='miter | round | bevel';
function drawStar(ctx, r, R, x, y, rot=0) {
var drawStarLine = (angle, radius) => {
ctx.lineTo(Math.cos(angle) * radius + x, -Math.sin(angle) * radius + y);
}
ctx.beginPath();
for(var i = 0; i < 5; i++) {
var ang = (18 + i * 72 - rot) / 180 * Math.PI;
drawStarLine(ang, R);
ang = (54 + i * 72 - rot) / 180 * Math.PI;
drawStarLine(ang, r);
}
ctx.closePath();
ctx.fillStyle='#fb3';
ctx.strokeStyle='#fd5';
ctx.lineWidth=3;
ctx.lineJoin = "round";
ctx.fill();
ctx.stroke();
}
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext("2d");
context.fillStyle = 'black';
context.fillRect(0, 0, canvas.width, canvas.height);
for(var i = 0; i < 200; i++) {
var r = Math.random() * 10 + 20;
var x = Math.random() * canvas.width;
var y = Math.random() * canvas.height;
drawStar(context, r/2.0, r, x, y);
}
}
translate(x, y)
rotate(deg)
scale(sx, sy)
function starPath(ctx) {
var drawStarLine = (angle, radius) => {
ctx.lineTo(Math.cos(angle) * radius, -Math.sin(angle) * radius);
}
ctx.beginPath();
for(var i = 0; i < 5; i++) {
var ang = (18 + i * 72) / 180 * Math.PI;
drawStarLine(ang, 1);
ang = (54 + i * 72) / 180 * Math.PI;
drawStarLine(ang, 0.5);
}
ctx.closePath();
}
function drawStar(ctx, x, y, R, rot) {
ctx.save();
ctx.translate(x, y);
ctx.rotate(rot / 180 * Math.PI);
ctx.scale(R, R);
starPath(ctx);
ctx.fillStyle = '#fb3';
ctx.fill();
ctx.restore();
}
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext("2d");
for(var i = 0; i < 200; i ++) {
var r = Math.random() * 180;
var R = Math.random();
var x = Math.random() * canvas.width;
var y = Math.random() * canvas.height;
drawStar(context, x, y, R, r);
}
}
transform(a,b,c,d,e,f)
a, d —— 水平、垂直缩放
b, c —— 水平、垂直倾斜
e, f —— 水平、垂直位移
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext('2d');
context.fillStyle = 'red';
context.strokeStyle = '#058';
context.lineWidth = 5;
context.save();
context.transform(1.2, 0.2, 0.2, 1.5, 50, 20);
context.setTransform(1.2, 0.2, 0.2, 1.5, 50, 20);
context.fillRect(50, 50, 300, 300);
context.strokeRect(50, 50, 300, 300);
context.restore();
};
fillStyle
的使用
creatLineGradient(x0, y0, x1, y1)
addColorStop(index, color)
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext('2d');
var lineGrad = context.createLinearGradient(0, 0, 400, 400);
lineGrad.addColorStop(0.0, 'white');
lineGrad.addColorStop(0.25, 'yellow');
lineGrad.addColorStop(0.5, 'green');
lineGrad.addColorStop(0.75, 'blue');
lineGrad.addColorStop(1.0, 'black');
context.fillStyle = lineGrad;
context.fillRect(0, 0, 800, 800);
};
creatRadialGradient(x0, y0, x1, y1)
addColorStop(index, color)
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext('2d');
var radialGrad = context.createRadialGradient(400, 400, 0, 400, 400, 400);
radialGrad.addColorStop(0.0, 'white');
radialGrad.addColorStop(0.25, 'yellow');
radialGrad.addColorStop(0.5, 'green');
radialGrad.addColorStop(0.75, 'blue');
radialGrad.addColorStop(1.0, 'black');
context.fillStyle = radialGrad;
context.fillRect(0, 0, 800, 800);
};
createPattern(img | canvas | video, repeat-style)
function starPath(ctx) {
var drawStarLine = function(angle, radius) {
ctx.lineTo(Math.cos(angle) * radius, -Math.sin(angle) * radius);
};
ctx.beginPath();
for(var i = 0; i < 5; i++) {
var ang = (18 + i * 72) / 180 * Math.PI;
drawStarLine(ang, 1);
ang = (54 + i * 72) / 180 * Math.PI;
drawStarLine(ang, 0.5);
}
ctx.closePath();
}
function drawStar(ctx, x, y, R, rot) {
ctx.save();
ctx.translate(x, y);
ctx.rotate(rot / 180 * Math.PI);
ctx.scale(R, R);
starPath(ctx);
ctx.fillStyle = '#fb3';
ctx.fill();
ctx.restore();
}
function createBackgroundCanvas() {
var backCanvas = document.createElement('canvas');
backCanvas.width = backCanvas.height = 100;
var backContext = backCanvas.getContext('2d');
drawStar(backContext, 50, 50, 50, 0);
return backCanvas;
}
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext('2d');
var backCanvas = createBackgroundCanvas();
var pattern = context.createPattern(backCanvas, "repeat");
context.fillStyle = pattern;
context.fillRect(0, 0, 800, 800);
};
context.arc(centerX, centerY, radius, startingAngle, endingAngle, anticlockwise=false)
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 1000;
var context = canvas.getContext('2d');
context.lineWidth = 5;
context.strokeStyle = '#058';
for (var i = 0; i < 10; i++) {
context.beginPath();
context.arc(50 + i * 100, 60, 40, 0, 2 * Math.PI * (i + 1) / 10);
context.stroke();
}
for (var i = 0; i < 10; i++) {
context.beginPath();
context.arc(50 + i * 100, 180, 40, 0, 2 * Math.PI * (i + 1) / 10);
context.closePath();
context.stroke();
}
for(var i = 0; i < 10; i++) {
context.beginPath();
context.arc(50 + i * 100, 300, 40, 0, 2 * Math.PI * (i + 1) / 10, true);
context.closePath();
context.stroke();
}
for(var i = 0; i < 10; i++) {
context.beginPath();
context.arc(50 + i * 100, 420, 40, 0, 2 * Math.PI * (i + 1) / 10, true);
context.stroke();
}
context.fillStyle = '#058';
for(var i = 0; i < 10; i++) {
context.beginPath();
context.arc(50 + i * 100, 540, 40, 0, 2 * Math.PI * (i + 1) / 10);
context.closePath();
context.fill();
}
for(var i = 0; i < 10; i++) {
context.beginPath();
context.arc(50 + i * 100, 660, 40, 0, 2 * Math.PI * (i + 1) / 10, true);
context.fill();
}
};
Rounded Corner Rectangle
function pathRoundRect(ctx, width, height, radius) {
ctx.beginPath();
ctx.arc(radius, radius, radius, Math.PI, 1.5 * Math.PI);
ctx.lineTo(width - 2 * radius, 0);
ctx.arc(width - radius, radius, radius, 1.5 * Math.PI, 2 * Math.PI);
ctx.lineTo(width, height - radius);
ctx.arc(width - radius, height - radius, radius, 0, 0.5 * Math.PI);
ctx.lineTo(radius, height);
ctx.arc(radius, height - radius, radius, 0.5 * Math.PI, Math.PI);
ctx.closePath();
}
function fillRoundRect(ctx, x, y, width, height, radius, fillColor) {
if ( 2 * radius > width || 2 * radius > height)
return;
ctx.save();
ctx.translate(x, y);
pathRoundRect(ctx, width, height, radius);
ctx.fillStyle = fillColor || "black";
ctx.fill();
ctx.restore();
}
function drawRoundRect(ctx, x, y, width, height, radius, lineWidth, strokeColor) {
if ( 2 * radius > width || 2 * radius > height)
return;
ctx.save();
ctx.translate(x, y);
pathRoundRect(ctx, width, height, radius);
ctx.lineWidth = lineWidth || 1;
ctx.strokeStyle = strokeColor || 'black';
ctx.stroke();
ctx.restore();
}
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 1000;
var context = canvas.getContext('2d');
drawRoundRect(context, 150, 150, 500, 500, 10, "#bbada0");
for (var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
fillRoundRect(context, 170 + i * 120, 170 + j * 120, 100, 100, 6, "#ccc0b3");
}
}
};
context.arcTo(x1, y1, x2, y2, radius)
function arcToTest(ctx, x0, y0, x1, y1, x2, y2, R) {
ctx.beginPath();
ctx.moveTo(x0, y0);
ctx.arcTo(x1, y1, x2, y2, R);
ctx.lineWidth = 6;
ctx.strokeStyle = "red";
ctx.stroke();
ctx.beginPath();
ctx.moveTo(x0, y0);
ctx.lineTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.lineWidth = 2;
ctx.strokeStyle = 'gray';
ctx.stroke();
}
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext('2d');
arcToTest(context, 150, 100, 650, 100, 650, 600, 300);
};
function dis(x0, y0, x1, y1) {
return Math.sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
}
function pathMoon(ctx, d) {
ctx.beginPath();
ctx.arc(0, 0, 1, 0.5 * Math.PI, 1.5 * Math.PI, true);
ctx.moveTo(0, -1);
ctx.arcTo(d, 0, 0, 1, dis(0, -1, d, 0) / d);
ctx.closePath();
}
function fillmoon(ctx, d, x, y, R, rot, fillColor) {
ctx.save();
ctx.translate(x, y);
ctx.rotate(rot * Math.PI / 180);
ctx.scale(R, R);
pathMoon(ctx, d);
ctx.fillStyle = fillColor || "#fb5";
ctx.fill();
ctx.restore();
}
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext('2d');
fillmoon(context, 2, 400, 400, 300, 30);
};
context.quadraticCurveTo(x1, y1, x2, y2)
Canvas Quadratic Curve Example
function dis(x0, y0, x1, y1) {
return Math.sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
}
function pathMoon(ctx, d) {
ctx.beginPath();
ctx.arc(0, 0, 1, 0.5 * Math.PI, 1.5 * Math.PI, true);
ctx.moveTo(0, -1);
//ctx.arcTo(d, 0, 0, 1, dis(0, -1, d, 0) / d);
ctx.quadraticCurveTo(d, 0, 0, 1);
ctx.closePath();
}
function fillmoon(ctx, d, x, y, R, rot, fillColor) {
ctx.save();
ctx.translate(x, y);
ctx.rotate(rot * Math.PI / 180);
ctx.scale(R, R);
pathMoon(ctx, d);
ctx.fillStyle = fillColor || "#fb5";
ctx.fill();
ctx.restore();
}
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext('2d');
fillmoon(context, 1.2, 400, 400, 300, 30);
};
BezierCurveTo(x1, y1, x2, y2, x3, y3);
function drawLand(ctx) {
ctx.save();
ctx.beginPath();
ctx.moveTo(0, 600);
ctx.bezierCurveTo(540, 400, 660, 800, 1200, 600);
ctx.lineTo(1200, 800);
ctx.lineTo(0, 800);
ctx.closePath();
var landStyle = ctx.createLinearGradient(0, 800, 0, 0);
landStyle.addColorStop(0.0, '#030');
landStyle.addColorStop(1.0, '#580');
ctx.fillStyle = landStyle;
ctx.fill();
ctx.restore();
}
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext('2d');
drawLand(context);
};
fillText(string, x, y)
strokeText(string, x, y)
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext('2d');
context.font = "bold 40px Arial";
context.fillStyle = '#058';
context.fillText("Hello World!", 40, 100);
context.lineWidth = 1;
context.strokeStyle = '#058';
context.strokeText("你好,世界!", 40, 200);
context.fillText("Hello World!", 40, 300, 150);
context.strokeText("你好,世界!", 40, 400, 150);
var lineGrad = context.createLinearGradient(0, 0, 300, 0);
lineGrad.addColorStop(0.0, 'red');
lineGrad.addColorStop(0.25, 'orange');
lineGrad.addColorStop(0.5, 'yellow');
lineGrad.addColorStop(0.75, 'green');
lineGrad.addColorStop(1.0, 'purple');
context.fillStyle = lineGrad;
context.fillText("Hello World!", 40, 500);
};
context.font = "font-style font-variant font-weight font-size font-family"
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext('2d');
context.fillStyle = '#058';
context.font = "normal 40px sans-serif";
context.fillText("hello world!", 40, 100);
context.font = "italic 40px sans-serif";
context.fillText("hello world!", 40, 200);
context.font = "oblique 40px sans-serif";
context.fillText("hello world!", 40, 300);
};
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext('2d');
context.fillStyle = '#058';
context.font = "bold 40px sans-serif";
context.fillText("Hello World!", 40, 100);
context.font = "small-caps bold 40px sans-serif";
context.fillText("Hello World!", 40, 200);
};
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext('2d');
context.fillStyle = '#058';
context.font = "lighter 40px sans-serif";
context.fillText("hello world!", 40, 100);
context.font = "normal 40px sans-serif";
context.fillText("hello world!", 40, 200);
context.font = "bold 40px sans-serif";
context.fillText("hello world!", 40, 300);
context.font = "bolder 40px sans-serif";
context.fillText("hello world!", 40, 400);
};
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext('2d');
context.fillStyle = '#058';
context.font = "xx-small sans-serif";
context.fillText("hello world!", 40, 100);
context.font = "x-small sans-serif";
context.fillText("hello world!", 40, 200);
context.font = "medium sans-serif";
context.fillText("hello world!", 40, 300);
context.font = "large sans-serif";
context.fillText("hello world!", 40, 400);
context.font = "x-large sans-serif";
context.fillText("hello world!", 40, 500);
context.font = "xx-large sans-serif";
context.fillText("hello world!", 40, 600);
};
context.textAlign = "left | center | right"
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext('2d');
context.fillStyle = '#058';
context.font = "bold 40px sans-serif";
context.textAlign = 'left';
context.fillText("textAlign = left", 400, 100);
context.textAlign = 'center';
context.fillText("textAlign = center", 400, 200);
context.textAlign = 'right';
context.fillText("textAlign = right", 400, 300);
context.strokeStyle = "#888";
context.moveTo(400, 0);
context.lineTo(400, 400);
context.stroke();
};
context.textBaseline = "top | middle | bottom | alphabetic(Default) | ideographic(汉字基准) | hanging(印度基准)"
function drawBaseline(ctx, height) {
ctx.save();
ctx.strokeStyle = "#888";
ctx.moveTo(0, height);
ctx.lineTo(800, height);
ctx.stroke();
ctx.restore();
}
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext('2d');
context.fillStyle = '#058';
context.font = "bold 40px sans-serif";
context.textBaseline = 'top';
context.fillText("textBaseline = top", 100, 100);
drawBaseline(context, 100);
context.textBaseline = 'middle';
context.fillText("textBaseline = middle", 100, 200);
drawBaseline(context, 200);
context.textBaseline = 'bottom';
context.fillText("textBaseline = bottom", 100, 300);
drawBaseline(context, 300);
context.textBaseline = 'alphabetic';
context.fillText("Hello, 世界!", 100, 400);
drawBaseline(context, 400);
context.textBaseline = 'ideographic';
context.fillText("Hello, 世界!", 100, 500);
drawBaseline(context, 500);
context.textBaseline = 'hanging';
context.fillText("Hello, 世界!", 100, 600);
drawBaseline(context, 600);
};
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext('2d');
context.fillStyle = '#058';
context.font = "bold 120px Arial";
context.textAlign = 'center';
context.textBaseline = 'middle';
context.fillText("CANVAS", canvas.width / 2, canvas.height / 2);
context.strokeStyle = "#888";
context.moveTo(0, canvas.height / 2);
context.lineTo(canvas.width, canvas.height / 2);
context.moveTo(canvas.width / 2, 0);
context.lineTo(canvas.width / 2, canvas.height);
context.stroke();
};
context.measureText(string).width
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext('2d');
context.fillStyle = '#058';
context.font = "bold 40px sans-serif";
context.fillText("Hello 世界!", 40, 100);
var textWidth = context.measureText("Hello 世界!").width;
context.fillText("the length of the string is " + textWidth + "px", 40, 200);
};
context.shadowColor
context.shadowOffsetX
context.shadowOffsetY
context.shadowBlur
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = canvas.height = 800;
var context = canvas.getContext('2d');
context.fillStyle = '#058';
context.shadowColor = "gray";
context.shadowOffsetX = context.shadowOffsetY = 20;
context.shadowBlur = 5;
context.fillRect(200, 200, 400, 400);
};
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = 1200;
canvas.height = 800;
var context = canvas.getContext('2d');
context.fillStyle = '#058';
context.globalAlpha = 0.7;
for(var i = 0; i < 100; i++) {
var R = Math.floor(Math.random() * 255);
var G = Math.floor(Math.random() * 255);
var B = Math.floor(Math.random() * 255);
context.fillStyle = "rgb(" + R + ", " + G + ", " + B + ")";
context.beginPath();
context.arc(Math.random() * canvas.width, Math.random() * canvas.height, Math.random() * 100, 0, Math.PI * 2);
context.fill();
}
};
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = 1200;
canvas.height = 800;
var context = canvas.getContext('2d');
context.fillStyle = 'blue';
context.fillRect(100, 200, 400, 400);
context.globalCompositeOperation = "destination-over";
context.fillStyle = 'red';
context.beginPath();
context.moveTo(400, 300);
context.lineTo(650, 700);
context.lineTo(150, 700);
context.closePath();
context.fill();
};
var balls = new Array(100);
function draw(ctx) {
var canvas = ctx.canvas;
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < balls.length; i++) {
ctx.globalCompositeOperation = 'lighter';
ctx.fillStyle = balls[i].color;
ctx.beginPath();
ctx.arc(balls[i].x, balls[i].y, balls[i].radius, 0, 2*Math.PI);
ctx.closePath();
ctx.fill();
}
}
function update(canvasWidth, canvasHeight) {
for(var i = 0; i < balls.length; i++) {
balls[i].x += balls[i].vx;
balls[i].y += balls[i].vy;
if(balls[i].x - balls[i].radius <= 0) {
balls[i].vx = -balls[i].vx;
balls[i].x = balls[i].radius;
}
if(balls[i].x + balls[i].radius >= canvasWidth) {
balls[i].vx = - balls[i].vx;
balls[i].x = canvasWidth - balls[i].radius;
}
if(balls[i].y - balls[i].radius <= 0) {
balls[i].vy = - balls[i].vy;
balls[i].y = balls[i].radius;
}
if(balls[i].y + balls[i].radius >= canvasHeight) {
balls[i].vy = -balls[i].vy;
balls[i].y = canvasHeight - balls[i].radius;
}
}
}
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = 1200;
canvas.height = 800;
var context = canvas.getContext('2d');
context.fillStyle = '#058';
context.globalAlpha = 0.7;
for(var i = 0; i < balls.length; i++) {
var R = Math.floor(Math.random() * 255);
var G = Math.floor(Math.random() * 255);
var B = Math.floor(Math.random() * 255);
var radius = Math.random() * 50 + 20;
balls[i] = {
color: "rgb(" + R + ", " + G + ", " + B + ")",
radius: radius,
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
vx: (Math.random()) * Math.pow(-1, Math.floor(Math.random() * 100)),
vy: (Math.random()) * Math.pow(-1, Math.floor(Math.random() * 100))
};
}
setInterval(function() {
draw(context);
update(canvas.width, canvas.height);
}, 50);
};
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = 1200;
canvas.height = 800;
var context = canvas.getContext('2d');
context.beginPath();
context.fillStype = "#000";
context.fillRect(0, 0, canvas.width, canvas.height);
context.beginPath();
context.fillStyle = "#fff";
context.arc(canvas.width / 2, canvas.height / 2, 200, 0, Math.PI * 2);
context.fill();
context.clip();
context.font = "bold 150px Arial";
context.textAlign = 'center';
context.textBaseline = 'middle';
context.fillStyle = '#058';
context.fillText("CANVAS", canvas.width / 2, canvas.height / 2);
};
非零环绕原则
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = 800;
canvas.height = 800;
var context = canvas.getContext('2d');
context.beginPath();
context.arc(400, 400, 300, 0, Math.PI * 2, false);
context.arc(400, 400, 150, 0, Math.PI * 2, true);
context.closePath();
context.fillStyle = "#058";
context.shadowColor = "gray";
context.shadowOffsetX = context.shadowOffsetY = 10;
context.shadowBlur = 10;
context.fill();
};
context.clearRect
context.isPointInPath(x, y)
var balls = [];
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
window.onload = function() {
canvas.width = 800;
canvas.height = 800;
for (var i = 0; i < 10; i++) {
var aBall = {
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
r: Math.random() * 50 + 20
};
balls[i] = aBall;
}
draw();
canvas.addEventListener("mouseup", detect);
};
function draw() {
for (var i = 0; i < balls.length; i++) {
context.beginPath();
context.arc(balls[i].x, balls[i].y, balls[i].r, 0, Math.PI*2);
context.fillStyle = '#058';
context.fill();
}
}
function detect(event) {
var x = event.clientX - canvas.getBoundingClientRect().left;
var y = event.clientY - canvas.getBoundingClientRect().top;
for(var i = 0; i < balls.length; i++) {
context.beginPath();
context.arc(balls[i].x, balls[i].y, balls[i].r, 0, Math.PI * 2);
if (context.isPointInPath(x, y)) {
context.fillStyle = 'red';
context.fill();
}
}
}