ç§ãã¡ã <canvas>
è¦ç´ ã®æä½ã« JavaScript ã使ãã®ã¯ãã¨ã¦ãç°¡åã«ã¤ã³ã¿ã©ã¯ãã£ããªã¢ãã¡ã¼ã·ã§ã³ã使ã§ããããã§ããæ¬ç« ã§ã¯ãããã¤ãã®åºæ¬çãªã¢ãã¡ã¼ã·ã§ã³ã§ããã®æ¦è¦ãã¤ããã§ããã¾ãã
ããããæå¤§ã®å¶ç´ã¯ããã£ã³ãã¹ã«å³å½¢ãä¸åº¦æç»ããã¨ããã®ç¶æ ãç¶æããããã¨ã§ããã¢ãã¡ã¼ã·ã§ã³ãããå ´åã«ããç§»åããé¨åã¨ä»¥åã«æããé¨åããã¹ã¦åæç»ããå¿ è¦ãããã¾ããè¤éãªãã¬ã¼ã ã®åæç»ã«ã¯æéãããããããã©ã¼ãã³ã¹ã¯ãå®è¡ãã¦ããã³ã³ãã¥ã¼ã¿ã¼ã®é度ã«å¤§ããä¾åãã¾ãã
åºæ¬çãªã¢ãã¡ã¼ã·ã§ã³ã®æé ãã¬ã¼ã ãæç»ãããæé ã¯ããã®ããã«ãªãã¾ãã
clearRect()
ã¡ã½ããã使ããã¨ã§ããå³å½¢ã¯ããã£ã³ãã¹ã®ã¡ã½ãããç´æ¥ä½¿ç¨ããããã«ã¹ã¿ã 颿°ãå¼ã³åºããã¨ã«ãã£ã¦æç»ããã¾ããé常ã¯ãã¹ã¯ãªããã®å®è¡ãçµäºããã¨ãã«ã®ã¿ããããã®çµæããã£ã³ãã¹ã«è¡¨ç¤ºããã¾ãããã¨ãã°ãfor
ã«ã¼ãå
ããã¢ãã¡ã¼ã·ã§ã³ãå®è¡ãããã¨ã¯ã§ãã¾ããã
ã¤ã¾ããä¸å®ã®æéãã¨ã«æç»é¢æ°ãå®è¡ããæ¹æ³ãå¿ è¦ã§ãããã®ãããªã¢ãã¡ã¼ã·ã§ã³ãå¶å¾¡ããã«ã¯ã 2 ã¤ã®æ¹æ³ãããã¾ãã
ã¹ã±ã¸ã¥ã¼ã«ã®æ´æ°ã¾ããsetInterval()
ãsetTimeout()
ãããã¾ãããããããwindow.requestAnimationFrame()
颿°ãããã¾ãããããã¯ãç¹å®ã®é¢æ°ãä¸å®æéã§å¼ã³åºãããã«ä½¿ç¨ã§ãã¾ãã
setInterval()
function
ã§æå®ãã颿°ã delay
ããªç§ãã¨ã«ç¹°ãè¿ãå®è¡ãã¾ãã
setTimeout()
function
ã§æå®ãã颿°ã delay
ããªç§å¾ã«å®è¡ãã¾ãã
requestAnimationFrame(callback)
ã¢ãã¡ã¼ã·ã§ã³ãå®è¡ãããã¨ããã©ã¦ã¶ã¼ã«éç¥ããæ¬¡ã®åæç»ã®åã«ã¢ãã¡ã¼ã·ã§ã³ãæ´æ°ããããããã©ã¦ã¶ã¼ãæå®ã®é¢æ°ãå¼ã³åºãããã«è¦æ±ãã¾ãã
ã¦ã¼ã¶ã¼ã®æä½ãå¿
è¦ãªãå ´åã¯ãæä¾ãããã³ã¼ããç¹°ãè¿ãå®è¡ãã setInterval()
颿°ãå©ç¨ã§ãã¾ããã²ã¼ã ã使ãããå ´åããã¼ãã¼ãã¾ãã¯ãã¦ã¹ã®ã¤ãã³ãã使ç¨ããã¢ãã¡ã¼ã·ã§ã³ãå¶å¾¡ããããã«ã¯ setTimeout()
ãå©ç¨ã§ãã¾ããaddEventListener()
ãç¨ãã¦ã¤ãã³ããªã¹ãã¼ãè¨å®ãããã¨ã§ãã¦ã¼ã¶ã¼ã®æä½ãåå¾ããã¢ãã¡ã¼ã·ã§ã³é¢æ°ãå®è¡ãã¾ãã
ã¡ã¢: 以ä¸ã®ä¾ã§ã¯ãwindow.requestAnimationFrame()
ã¡ã½ããã使ç¨ãã¦ã¢ãã¡ã¼ã·ã§ã³ãå¶å¾¡ãã¾ããrequestAnimationFrame
ã¡ã½ããã¯ããã¬ã¼ã ãæç»ããæºåãã§ããæã«ã·ã¹ãã ãã¢ãã¡ã¼ã·ã§ã³ãã¬ã¼ã ãå¼ã³åºããã¨ã§ãããã¹ã ã¼ãºã§å¹ççãªæ¹æ³ã§ã¢ãã¡ã¼ã·ã§ã³ãæä¾ãã¾ããé常ãã³ã¼ã«ããã¯åæ°ã¯ 1 ç§ããã 60 åã¨ãªããããã¯ã°ã©ã¦ã³ãã¿ãã§å®è¡ãã¦ããå ´åã¯ãã¬ã¼ããä½ããªããã¨ãããã¾ããç¹ã«ã²ã¼ã ã®ã¢ãã¡ã¼ã·ã§ã³ã«ã¼ãã®è©³ç´°ã«ã¤ãã¦ã¯ãã²ã¼ã éçºã®ãããªã²ã¼ã ã®è§£åå¦ãåç
§ãã¦ãã ããã
ãã®ä¾ã¯ã太é½ç³»ã®å°ããªã¢ãã«ãã¢ãã¡ã¼ã·ã§ã³ããã¾ãã
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); // clear canvas
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}`;
// Write Hours
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);
çµæ
ã¡ã¢: æè¨ã¯ 1 ç§ã« 1 åããæ´æ°ããã¾ããããã¢ãã¡ã¼ã·ã§ã³ç»å㯠1 ç§éã« 60 ãã¬ã¼ã ï¼ã¾ãã¯ã¦ã§ããã©ã¦ã¶ã¼ã®è¡¨ç¤ºãªãã¬ãã·ã¥ã¬ã¼ãï¼ã§æ´æ°ããã¾ãã ç§éãã¹ã¤ã¼ãããã¦æè¨ã表示ããã«ã¯ãä¸è¨ã® const sec
ã®å®ç¾©ãã³ã¡ã³ãã¢ã¦ãããããã¼ã¸ã§ã³ã¨ç½®ãæãã¦ãã ããã
ãã®ä¾ã¯ãå·¦ããå³ã¸é¢¨æ¯åçãã¹ã¯ãã¼ã«ããã¾ããWikipedia ããã¨ã»ããå½ç«å ¬åã®ç»åã使ãã¾ãããããã£ã³ãã¹ããã大ããªä»»æã®ç»åã使ç¨ã§ãã¾ãã
HTMLãã® HTML ã«ã¯ãç»åãã¹ã¯ãã¼ã«è¡¨ç¤ºããã <canvas>
ãå«ã¾ãã¾ããããã§æå®ããå¹
ã¨é«ãã¯ã JavaScript ã®ã³ã¼ãã«ãã夿° canvasXSize
㨠canvasYSize
ã®å¤ã¨ä¸è´ããå¿
è¦ããããã¨ã«æ³¨æãã¦ãã ããã
<canvas id="canvas" width="800" height="200"
>ã¨ã»ããå½ç«å
¬åãã¨ã«ã»ãã£ãã¿ã³ã®éºã®èå</canvas
>
JavaScript
const img = new Image();
// ã¦ã¼ã¶ã¼å¤æ°ãããããã«ã¹ã¿ãã¤ãºããã¨ãã¹ã¯ãã¼ã«ããç»åãæ¹åãé度ã
// 夿´ãããã¨ãã§ãã¾ãã
img.src = "capitan_meadows_yosemite_national_park.jpg";
const canvasXSize = 800;
const canvasYSize = 200;
const speed = 30; // å°ããããã¨éããªã
const scale = 1.05;
const y = -4.5; // åç´ãªãã»ãã
// ã¡ã¤ã³ããã°ã©ã
const dx = 0.75;
let imgW;
let imgH;
let x = 0;
let clearX;
let clearY;
let ctx;
img.onload = () => {
imgW = img.width * scale;
imgH = img.height * scale;
if (imgW > canvasXSize) {
// ãã£ã³ãã¹ãã大ããªç»å
x = canvasXSize - imgW;
}
// ç»åã®å¯¸æ³ããã£ã³ãã¹ãã大ãããã©ãã調ã¹ã
clearX = Math.max(imgW, canvasXSize);
clearY = Math.max(imgH, canvasYSize);
// ãã£ã³ãã¹ã³ã³ããã¹ãã®åå¾
ctx = document.getElementById("canvas").getContext("2d");
// ãªãã¬ãã·ã¥ã¬ã¼ãã®è¨å®
return setInterval(draw, speed);
};
function draw() {
ctx.clearRect(0, 0, clearX, clearY); // ãã£ã³ãã¹ãã¯ãªã¢
// ç»åããã£ã³ãã¹ã®å¤§ãã以ä¸ã®å ´å
if (imgW <= canvasXSize) {
// ãªã»ãããæåããå§ãã
if (x > canvasXSize) {
x = -imgW + x;
}
// 追å ã®ç»å 1 ãæç»
if (x > 0) {
ctx.drawImage(img, -imgW + x, y, imgW, imgH);
}
// 追å ã®ç»å 2 ãæç»
if (x - imgW > 0) {
ctx.drawImage(img, -imgW * 2 + x, y, imgW, imgH);
}
} else {
// ç»åããã£ã³ãã¹ã®å¤§ãããã大ãã
// ãªã»ãããæåããå§ãã
if (x > canvasXSize) {
x = canvasXSize - imgW;
}
// 追å ã®ç»åãæç»
if (x > canvasXSize - imgW) {
ctx.drawImage(img, x - imgW + 1, y, imgW, imgH);
}
}
// ç»åãæç»
ctx.drawImage(img, x, y, imgW, imgH);
// ç§»åé
x += dx;
}
çµæ ãã¦ã¹è¿½è·¡ã¢ãã¡ã¼ã·ã§ã³ HTML
<canvas id="cw"
>ç»åä¸ãç§»åããã«ã¼ã½ã«ã«ç¶ããå¤è²ã®æ¶ããå
ã®æµãã使ããã¢ãã¡ã¼ã·ã§ã³ã
ç»åä¸ãç§»åãããã«ã¼ã½ã«ã«è¿½å¾ããå¤è²ã®å
ã®æµã
</canvas>
CSS
#cw {
position: fixed;
z-index: -1;
}
body {
margin: 0;
padding: 0;
background-color: rgb(0 0 0 / 5%);
}
JavaScript
const canvas = document.getElementById("cw");
const context = canvas.getContext("2d");
context.globalAlpha = 0.5;
const cursor = {
x: innerWidth / 2,
y: innerHeight / 2,
};
let particlesArray = [];
generateParticles(101);
setSize();
anim();
addEventListener("mousemove", (e) => {
cursor.x = e.clientX;
cursor.y = e.clientY;
});
addEventListener(
"touchmove",
(e) => {
e.preventDefault();
cursor.x = e.touches[0].clientX;
cursor.y = e.touches[0].clientY;
},
{ passive: false },
);
addEventListener("resize", () => setSize());
function generateParticles(amount) {
for (let i = 0; i < amount; i++) {
particlesArray[i] = new Particle(
innerWidth / 2,
innerHeight / 2,
4,
generateColor(),
0.02,
);
}
}
function generateColor() {
let hexSet = "0123456789ABCDEF";
let finalHexString = "#";
for (let i = 0; i < 6; i++) {
finalHexString += hexSet[Math.ceil(Math.random() * 15)];
}
return finalHexString;
}
function setSize() {
canvas.height = innerHeight;
canvas.width = innerWidth;
}
function Particle(x, y, particleTrailWidth, strokeColor, rotateSpeed) {
this.x = x;
this.y = y;
this.particleTrailWidth = particleTrailWidth;
this.strokeColor = strokeColor;
this.theta = Math.random() * Math.PI * 2;
this.rotateSpeed = rotateSpeed;
this.t = Math.random() * 150;
this.rotate = () => {
const ls = {
x: this.x,
y: this.y,
};
this.theta += this.rotateSpeed;
this.x = cursor.x + Math.cos(this.theta) * this.t;
this.y = cursor.y + Math.sin(this.theta) * this.t;
context.beginPath();
context.lineWidth = this.particleTrailWidth;
context.strokeStyle = this.strokeColor;
context.moveTo(ls.x, ls.y);
context.lineTo(this.x, this.y);
context.stroke();
};
}
function anim() {
requestAnimationFrame(anim);
context.fillStyle = "rgb(0 0 0 / 5%)";
context.fillRect(0, 0, canvas.width, canvas.height);
particlesArray.forEach((particle) => particle.rotate());
}
çµæ ãã®ä»ã®ä¾
é«åº¦ãªã¢ãã¡ã¼ã·ã§ã³æè¡ã¨ç©ã®åãã«ã¤ãã¦è¦ã¦ããã¾ãã
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4