åºæ¬çå¨ç»
ç±äºæä»¬æ¯ç¨ JavaScript å»ææ§ <canvas> 对象ï¼è¿æ ·è¦å®ç°ä¸äºäº¤äºå¨ç»ä¹æ¯ç¸å½å®¹æçã卿¬ç« ä¸ï¼æä»¬å°ççå¦ä½åä¸äºåºæ¬çå¨ç»ã
å¯è½æå¤§çéå¶å°±æ¯å¾å䏿¦ç»å¶åºæ¥ï¼å®å°±æ¯ä¸ç´ä¿æé£æ ·äºã妿éè¦ç§»å¨å®ï¼æä»¬ä¸å¾ä¸å¯¹ææä¸è¥¿ï¼å æ¬ä¹åçï¼è¿è¡éç»ãéç»æ¯ç¸å½è´¹æ¶çï¼è䏿§è½å¾ä¾èµäºçµèçé度ã
å¨ç»çåºæ¬æ¥éª¤
ä½ å¯ä»¥éè¿ä»¥ä¸çæ¥éª¤æ¥ç»åºä¸å¸§ï¼
- æ¸
空 canvas
é¤éæ¥ä¸æ¥è¦ç»çå
容ä¼å®å
¨å
满 canvasï¼ä¾å¦èæ¯å¾ï¼ï¼å¦åä½ éè¦æ¸
空ææãæç®åç忳就æ¯ç¨
clearRectæ¹æ³ã - ä¿å canvas ç¶æ å¦æä½ è¦æ¹åä¸äºä¼æ¹å canvas ç¶æçè®¾ç½®ï¼æ ·å¼ï¼åå½¢ä¹ç±»çï¼ï¼åè¦å¨æ¯ç»ä¸å¸§ä¹æ¶é½æ¯åå§ç¶æçè¯ï¼ä½ éè¦å ä¿åä¸ä¸ã
- ç»å¶å¨ç»å¾å½¢ï¼animated shapesï¼ è¿ä¸æ¥ææ¯éç»å¨ç»å¸§ã
- æ¢å¤ canvas ç¶æ 妿已ç»ä¿åäº canvas çç¶æï¼å¯ä»¥å æ¢å¤å®ï¼ç¶åéç»ä¸ä¸å¸§ã
ææ§å¨ç»
å¨ canvas ä¸ç»å¶å 容æ¯ç¨ canvas æä¾çæè èªå®ä¹çæ¹æ³ï¼èéå¸¸ï¼æä»¬ä» ä» å¨èæ¬æ§è¡ç»æåæè½çè§ç»æï¼æ¯å¦è¯´ï¼å¨ for 循ç¯éé¢å宿å¨ç»æ¯ä¸å¤ªå¯è½çã
å æ¤ï¼ä¸ºäºå®ç°å¨ç»ï¼æä»¬éè¦ä¸äºå¯ä»¥å®æ¶æ§è¡éç»çæ¹æ³ãæä¸¤ç§æ¹æ³å¯ä»¥å®ç°è¿æ ·çå¨ç»ææ§ãé¦å
å¯ä»¥éè¿ setInterval å setTimeout æ¹æ³æ¥æ§å¶å¨è®¾å®çæ¶é´ç¹ä¸æ§è¡éç»ã
æå®æå°æ´æ°ç»å¸
é¦å
ï¼å¯ä»¥ç¨ setInterval()ãsetTimeout() å requestAnimationFrame() æ¥è®¾å®å®ææ§è¡ä¸ä¸ªæå®å½æ°ã
setInterval()-
å½è®¾å®å¥½é´éæ¶é´åï¼function ä¼å®ææ§è¡ã
setTimeout()-
å¨è®¾å®å¥½çæ¶é´ä¹åæ§è¡å½æ°
requestAnimationFrame()-
åè¯æµè§å¨ä½ 叿æ§è¡ä¸ä¸ªå¨ç»ï¼å¹¶å¨éç»ä¹åï¼è¯·æ±æµè§å¨æ§è¡ä¸ä¸ªç¹å®ç彿°æ¥æ´æ°å¨ç»ã
å¦æä½ å¹¶ä¸éè¦ä¸ç¨æ·äºå¨ï¼ä½ å¯ä»¥ä½¿ç¨ setInterval() æ¹æ³ï¼å®å°±å¯ä»¥å®ææ§è¡æå®ä»£ç ã妿æä»¬éè¦åä¸ä¸ªæ¸¸æï¼æä»¬å¯ä»¥ä½¿ç¨é®çæè
é¼ æ äºä»¶é
åä¸ setTimeout() æ¹æ³æ¥å®ç°ãéè¿è®¾ç½®äºä»¶çå¬ï¼æä»¬å¯ä»¥ææç¨æ·ç交äºï¼å¹¶æ§è¡ç¸åºçå¨ä½ã
夿³¨ï¼ä¸é¢çä¾åï¼éç¨ window.requestAnimationFrame()å®ç°å¨ç»ææãè¿ä¸ªæ¹æ³æä¾äºæ´å å¹³ç¼å¹¶æ´å ææççæ¹å¼æ¥æ§è¡å¨ç»ï¼å½ç³»ç»åå¤å¥½äºéç»æ¡ä»¶çæ¶åï¼æè°ç¨ç»å¶å¨ç»å¸§ãä¸è¬æ¯ç§éåè°å½æ°æ§è¡ 60 次ï¼ä¹æå¯è½ä¼è¢«éä½ãæ³è¦äºè§£æ´å¤å
³äºå¨ç»å¾ªç¯çä¿¡æ¯ï¼å°¤å
¶æ¯æ¸¸æï¼å¯ä»¥å¨Game development zone åèè¿ç¯æç« Anatomy of a video gameã
太é³ç³»çå¨ç»
该示ä¾å±ç¤ºäºä¸ä¸ªå°å太é³ç³»æ¨¡åçå¨ç»ã
HTML
<canvas id="canvas" width="300" height="300"></canvas>
JavaScript
const sun = new Image();
const moon = new Image();
const earth = new Image();
function init() {
sun.src = "canvas_sun.png";
moon.src = "canvas_moon.png";
earth.src = "canvas_earth.png";
window.requestAnimationFrame(draw);
}
function draw() {
const ctx = document.getElementById("canvas").getContext("2d");
ctx.globalCompositeOperation = "destination-over";
ctx.clearRect(0, 0, 300, 300); // æ¸
é¤ç»å¸
ctx.fillStyle = "rgb(0 0 0 / 40%)";
ctx.strokeStyle = "rgb(0 153 255 / 40%)";
ctx.save();
ctx.translate(150, 150);
// å°ç
const time = new Date();
ctx.rotate(
((2 * Math.PI) / 60) * time.getSeconds() +
((2 * Math.PI) / 60000) * time.getMilliseconds(),
);
ctx.translate(105, 0);
ctx.fillRect(0, -12, 40, 24); // é´å½±
ctx.drawImage(earth, -12, -12);
// æäº®
ctx.save();
ctx.rotate(
((2 * Math.PI) / 6) * time.getSeconds() +
((2 * Math.PI) / 6000) * time.getMilliseconds(),
);
ctx.translate(0, 28.5);
ctx.drawImage(moon, -3.5, -3.5);
ctx.restore();
ctx.restore();
ctx.beginPath();
ctx.arc(150, 150, 105, 0, Math.PI * 2, false); // å°ç轨é
ctx.stroke();
ctx.drawImage(sun, 0, 0, 300, 300);
window.requestAnimationFrame(draw);
}
init();
ç»æ
å¨ç»æ¶é
该示ä¾ç»å¶ä¸ä¸ªå¯ä»¥æ¾ç¤ºå½åæ¶é´çå¨ç»æ¶éã
HTML
<canvas id="canvas" width="150" height="150">å½åæ¶é´</canvas>
JavaScript
function clock() {
const now = new Date();
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.save();
ctx.clearRect(0, 0, 150, 150);
ctx.translate(75, 75);
ctx.scale(0.4, 0.4);
ctx.rotate(-Math.PI / 2);
ctx.strokeStyle = "black";
ctx.fillStyle = "white";
ctx.lineWidth = 8;
ctx.lineCap = "round";
// å°æ¶å»åº¦
ctx.save();
for (let i = 0; i < 12; i++) {
ctx.beginPath();
ctx.rotate(Math.PI / 6);
ctx.moveTo(100, 0);
ctx.lineTo(120, 0);
ctx.stroke();
}
ctx.restore();
// åéå»åº¦
ctx.save();
ctx.lineWidth = 5;
for (let i = 0; i < 60; i++) {
if (i % 5 !== 0) {
ctx.beginPath();
ctx.moveTo(117, 0);
ctx.lineTo(120, 0);
ctx.stroke();
}
ctx.rotate(Math.PI / 30);
}
ctx.restore();
const sec = now.getSeconds();
// è¦æ¾ç¤ºæ«ç§å¼çæ¶éï¼è¯·ä½¿ç¨ï¼
// const sec = now.getSeconds() + now.getMilliseconds() / 1000;
const min = now.getMinutes();
const hr = now.getHours() % 12;
ctx.fillStyle = "black";
// æ¾ç¤ºå¾åæè¿°
canvas.innerText = `å½åæ¶é´ï¼${hr}:${min}`;
// æ¶é
ctx.save();
ctx.rotate(
(Math.PI / 6) * hr + (Math.PI / 360) * min + (Math.PI / 21600) * sec,
);
ctx.lineWidth = 14;
ctx.beginPath();
ctx.moveTo(-20, 0);
ctx.lineTo(80, 0);
ctx.stroke();
ctx.restore();
// åé
ctx.save();
ctx.rotate((Math.PI / 30) * min + (Math.PI / 1800) * sec);
ctx.lineWidth = 10;
ctx.beginPath();
ctx.moveTo(-28, 0);
ctx.lineTo(112, 0);
ctx.stroke();
ctx.restore();
// ç§é
ctx.save();
ctx.rotate((sec * Math.PI) / 30);
ctx.strokeStyle = "#D40000";
ctx.fillStyle = "#D40000";
ctx.lineWidth = 6;
ctx.beginPath();
ctx.moveTo(-30, 0);
ctx.lineTo(83, 0);
ctx.stroke();
ctx.beginPath();
ctx.arc(0, 0, 10, 0, Math.PI * 2, true);
ctx.fill();
ctx.beginPath();
ctx.arc(95, 0, 10, 0, Math.PI * 2, true);
ctx.stroke();
ctx.fillStyle = "rgb(0 0 0 / 0%)";
ctx.arc(0, 0, 3, 0, Math.PI * 2, true);
ctx.fill();
ctx.restore();
ctx.beginPath();
ctx.lineWidth = 14;
ctx.strokeStyle = "#325FA2";
ctx.arc(0, 0, 142, 0, Math.PI * 2, true);
ctx.stroke();
ctx.restore();
window.requestAnimationFrame(clock);
}
window.requestAnimationFrame(clock);
ç»æ
夿³¨ï¼å°½ç®¡æ¶éæ¯ç§åªæ´æ°ä¸æ¬¡ï¼ä½å¨ç»å¾åæ¯ç§æ´æ° 60 æ¬¡ï¼æè
ä»¥ä½ ç Web æµè§å¨çæ¾ç¤ºå·æ°çï¼ãè¦ä½¿ç¨æ«æå¼çæ¶éï¼è¯·å°ä¸é¢ç const sec å®ä¹æ¿æ¢ä¸ºå·²æ³¨éççæ¬ã
循ç¯å ¨æ¯ç §ç
å¨è¿ä¸ªä¾åä¸ï¼ä¼æä¸ä¸ªèªå·¦å峿»å¨çå ¨æ¯å¾ãæä»¬ä½¿ç¨äºå¨ç»´åºç¾ç§ä¸æ¾å°çå°¤å¡ç±³æå½å®¶å ¬åçå¾çï¼å½ç¶ä½ å¯ä»¥éææ¾ä¸å¼ ä»»ä½å°ºå¯¸å¤§äº canvas çå¾çã
var img = new Image();
// User Variables - customize these to change the image being scrolled, its
// direction, and the speed.
img.src = "capitan_meadows_yosemite_national_park.jpg";
var CanvasXSize = 800;
var CanvasYSize = 200;
var speed = 30; // lower is faster
var scale = 1.05;
var y = -4.5; // vertical offset
// Main program
var dx = 0.75;
var imgW;
var imgH;
var x = 0;
var clearX;
var clearY;
var ctx;
img.onload = function () {
imgW = img.width * scale;
imgH = img.height * scale;
if (imgW > CanvasXSize) {
// image larger than canvas
x = CanvasXSize - imgW;
}
if (imgW > CanvasXSize) {
// image width larger than canvas
clearX = imgW;
} else {
clearX = CanvasXSize;
}
if (imgH > CanvasYSize) {
// image height larger than canvas
clearY = imgH;
} else {
clearY = CanvasYSize;
}
// get canvas context
ctx = document.getElementById("canvas").getContext("2d");
// set refresh rate
return setInterval(draw, speed);
};
function draw() {
ctx.clearRect(0, 0, clearX, clearY); // clear the canvas
// if image is <= Canvas Size
if (imgW <= CanvasXSize) {
// reset, start from beginning
if (x > CanvasXSize) {
x = -imgW + x;
}
// draw additional image1
if (x > 0) {
ctx.drawImage(img, -imgW + x, y, imgW, imgH);
}
// draw additional image2
if (x - imgW > 0) {
ctx.drawImage(img, -imgW * 2 + x, y, imgW, imgH);
}
}
// image is > Canvas Size
else {
// reset, start from beginning
if (x > CanvasXSize) {
x = CanvasXSize - imgW;
}
// draw aditional image
if (x > CanvasXSize - imgW) {
ctx.drawImage(img, x - imgW + 1, y, imgW, imgH);
}
}
// draw image
ctx.drawImage(img, x, y, imgW, imgH);
// amount to move
x += dx;
}
䏿¹å°±æ¯æ¯å¾çå¨å
¶ä¸æ»å¨ç <canvas>ãéè¦æ³¨æçæ¯è¿éå®ä¹ç width å height å¿
é¡»ä¸ JavaScript 代ç ä¸çåéå¼CanvasXZSizeåCanvasYSizeä¿æä¸è´ã
<canvas id="canvas" width="800" height="200"></canvas>
é¼ æ 追踪å¨ç»
<!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>Document</title>
<script>
var cn;
//= document.getElementById('cw');
var c;
var u = 10;
const m = {
x: innerWidth / 2,
y: innerHeight / 2,
};
window.onmousemove = function (e) {
m.x = e.clientX;
m.y = e.clientY;
};
function gc() {
var s = "0123456789ABCDEF";
var c = "#";
for (var i = 0; i < 6; i++) {
c += s[Math.ceil(Math.random() * 15)];
}
return c;
}
var a = [];
window.onload = function myfunction() {
cn = document.getElementById("cw");
c = cn.getContext("2d");
for (var i = 0; i < 10; i++) {
var r = 30;
var x = Math.random() * (innerWidth - 2 * r) + r;
var y = Math.random() * (innerHeight - 2 * r) + r;
var t = new ob(
innerWidth / 2,
innerHeight / 2,
5,
"red",
Math.random() * 200 + 20,
2,
);
a.push(t);
}
//cn.style.backgroundColor = "#700bc8";
c.lineWidth = "2";
c.globalAlpha = 0.5;
resize();
anim();
};
window.onresize = function () {
resize();
};
function resize() {
cn.height = innerHeight;
cn.width = innerWidth;
for (var i = 0; i < 101; i++) {
var r = 30;
var x = Math.random() * (innerWidth - 2 * r) + r;
var y = Math.random() * (innerHeight - 2 * r) + r;
a[i] = new ob(
innerWidth / 2,
innerHeight / 2,
4,
gc(),
Math.random() * 200 + 20,
0.02,
);
}
// a[0] = new ob(innerWidth / 2, innerHeight / 2, 40, "red", 0.05, 0.05);
//a[0].dr();
}
function ob(x, y, r, cc, o, s) {
this.x = x;
this.y = y;
this.r = r;
this.cc = cc;
this.theta = Math.random() * Math.PI * 2;
this.s = s;
this.o = o;
this.t = Math.random() * 150;
this.o = o;
this.dr = function () {
const ls = {
x: this.x,
y: this.y,
};
this.theta += this.s;
this.x = m.x + Math.cos(this.theta) * this.t;
this.y = m.y + Math.sin(this.theta) * this.t;
c.beginPath();
c.lineWidth = this.r;
c.strokeStyle = this.cc;
c.moveTo(ls.x, ls.y);
c.lineTo(this.x, this.y);
c.stroke();
c.closePath();
};
}
function anim() {
requestAnimationFrame(anim);
c.fillStyle = "rgba(0,0,0,0.05)";
c.fillRect(0, 0, cn.width, cn.height);
a.forEach(function (e, i) {
e.dr();
});
}
</script>
<style>
#cw {
position: fixed;
z-index: -1;
}
body {
margin: 0;
padding: 0;
background-color: rgba(0, 0, 0, 0.05);
}
</style>
</head>
<body>
<canvas id="cw"></canvas>
qwerewr
</body>
</html>
Output
å ¶ä»ä¾å
- Advanced animations
-
æä»¬å°å¨ä¸ä¸ç« çå°ä¸äºå è¿çå¨ç»ææ¯åç©çç°è±¡ã