canvas JS technology application examples: five cases you have to know
Introduction:
The emergence of HTML5 has brought new possibilities to web development , especially the Canvas element, which provides a powerful ability to draw graphics and animations on the page. Combined with the power of JavaScript, developers can use Canvas to achieve a variety of cool effects and interactions, and improve user experience. This article introduces five amazing examples of Canvas JS applications and provides corresponding code examples.
1. Real-time data visualization charts
In practical applications, we often need to display a large amount of data in the form of charts. It is a common requirement to use Canvas and JavaScript to implement real-time data visualization charts. The following is a sample code for drawing a line chart:
// 创建 Canvas 元素 var canvas = document.getElementById("chart"); var ctx = canvas.getContext("2d"); // 定义坐标轴 ctx.beginPath(); ctx.moveTo(50, 50); ctx.lineTo(50, 300); ctx.lineTo(500, 300); ctx.stroke(); // 绘制数据点 var data = [30, 40, 60, 80, 50, 20]; var unitX = 20; // 数据点在 X 轴上的间距 var scale = 2; // 数据点在 Y 轴上的比例尺 ctx.beginPath(); ctx.moveTo(50, 300 - data[0] * scale); for (var i = 1; i < data.length; i++) { ctx.lineTo(50 + i * unitX, 300 - data[i] * scale); } ctx.stroke();
2. Animated particle effect
Canvas can also be used to create a variety of cool animation effects, of which animated particle effects are one of them. This effect is achieved by creating multiple particles that can move freely and then updating their positions every frame. The following is a simple example:
// 创建 Canvas 元素 var canvas = document.getElementById("particles"); var ctx = canvas.getContext("2d"); // 定义粒子 var particles = []; for (var i = 0; i < 100; i++) { particles.push({ x: Math.random() * canvas.width, y: Math.random() * canvas.height, vx: Math.random() * 2 - 1, vy: Math.random() * 2 - 1, size: Math.random() * 5 + 1, color: "#fff" }); } // 更新粒子位置并绘制 function update() { ctx.clearRect(0, 0, canvas.width, canvas.height); for (var i = 0; i < particles.length; i++) { var p = particles[i]; p.x += p.vx; p.y += p.vy; ctx.beginPath(); ctx.arc(p.x, p.y, p.size, 0, 2 * Math.PI); ctx.fillStyle = p.color; ctx.fill(); } requestAnimationFrame(update); } update();
3. Jigsaw Game
Canvas can be used to easily create various game effects, of which jigsaw puzzles are a good example. A fun jigsaw puzzle can be created by splitting an image into pieces, shuffling them out of order, and then letting the user click and drag to restore the image. The following is a simple sample code:
// 创建 Canvas 元素 var canvas = document.getElementById("puzzle"); var ctx = canvas.getContext("2d"); // 加载图片并分割成若干块 var image = new Image(); image.src = "puzzle.jpg"; image.onload = function() { var pieceWidth = image.width / 4; var pieceHeight = image.height / 4; // 打乱拼图块的顺序 // 绘制拼图 for (var i = 0; i < 4; i++) { for (var j = 0; j < 4; j++) { ctx.drawImage(image, j * pieceWidth, i * pieceHeight, pieceWidth, pieceHeight, j * pieceWidth, i * pieceHeight, pieceWidth, pieceHeight); } } }
4. Mobile drawing board application
Canvas can also be used to implement various drawing applications, such as mobile drawing board. Users can draw on Canvas, including drawing lines, drawing circles, filling colors, and other operations. The following is a sample code:
// 创建 Canvas 元素 var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); // 初始化绘图参数 ctx.lineWidth = 5; ctx.strokeStyle = "#000"; ctx.fillStyle = "#f00"; // 监听鼠标事件 var isDrawing = false; canvas.addEventListener("mousedown", function(e) { isDrawing = true; ctx.beginPath(); ctx.moveTo(e.clientX, e.clientY); }); canvas.addEventListener("mousemove", function(e) { if (!isDrawing) return; ctx.lineTo(e.clientX, e.clientY); ctx.stroke(); }); canvas.addEventListener("mouseup", function(e) { isDrawing = false; });
5. Mini Game: Brick Breaker
Canvas can not only be used to create static graphics, but updating images between multiple frames can achieve complex game effects. . The mini-game "Brick Breaker" is one example. This game is implemented by detecting collisions and updating the positions of the balls and bricks. The following is a sample code:
// 创建 Canvas 元素 var canvas = document.getElementById("game"); var ctx = canvas.getContext("2d"); // 初始化游戏参数 var ball = { x: canvas.width / 2, y: canvas.height - 30, dx: 2, dy: -2, radius: 10, color: "#0095DD" } var paddle = { x: canvas.width / 2 - 50, y: canvas.height - 10, width: 100, height: 10, color: "#0095DD" } var bricks = []; var brickRowCount = 3; var brickColumnCount = 5; for (var c = 0; c < brickColumnCount; c++) { for (var r = 0; r < brickRowCount; r++) { bricks.push({ x: c * (75 + 10) + 30, y: r * (20 + 10) + 30, width: 75, height: 20, color: "#0095DD" }); } } // 绘制游戏元素 function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.beginPath(); ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2); ctx.fillStyle = ball.color; ctx.fill(); ctx.closePath(); ctx.beginPath(); ctx.rect(paddle.x, paddle.y, paddle.width, paddle.height); ctx.fillStyle = paddle.color; ctx.fill(); ctx.closePath(); for (var i = 0; i < bricks.length; i++) { var brick = bricks[i]; ctx.beginPath(); ctx.rect(brick.x, brick.y, brick.width, brick.height); ctx.fillStyle = brick.color; ctx.fill(); ctx.closePath(); } } // 游戏循环 function gameLoop() { draw(); requestAnimationFrame(gameLoop); } gameLoop();
Conclusion:
Through Canvas JS technology, we can achieve a variety of stunning effects and interactions. This article introduces five common application examples, including real-time data visualization charts, animated particle effects, jigsaw puzzles, mobile sketchpad applications, and the mini game "Brick Breaker." These examples demonstrate the power and creativity of Canvas JS technology. We hope that through these sample codes, readers can gain a deeper understanding of Canvas JS technologies and apply them in their own projects.
The above is the detailed content of Five must-know cases to understand canvas JS technology. For more information, please follow other related articles on the PHP Chinese website!