Home > Article > Web Front-end > Practical method for interactive drawing using Canvas engine
Dynamic Interaction: Practical Tips for Canvas Engine to Implement Interactive Drawing
Introduction:
In modern Web development, more and more web page effects are required It has interactivity and animation effects, and the Canvas engine is one of our important tools to achieve these effects. This article will introduce some practical tips and techniques to help developers master the ability of the Canvas engine to implement interactive drawing. The following will introduce in detail how to use the Canvas engine to implement interactive drawing, with specific code examples.
1. Basic drawing and animation implementation
<canvas id="myCanvas" width="500" height="500"></canvas>
var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d");
// 绘制矩形 ctx.fillStyle = "red"; ctx.fillRect(10, 10, 100, 100); // 绘制圆形 ctx.fillStyle = "blue"; ctx.beginPath(); ctx.arc(250, 250, 50, 0, 2*Math.PI); ctx.fill(); // 绘制直线 ctx.strokeStyle = "green"; ctx.beginPath(); ctx.moveTo(50, 50); ctx.lineTo(200, 200); ctx.stroke();
// 清空Canvas function clearCanvas() { ctx.clearRect(0, 0, canvas.width, canvas.height); } // 绘制平移动画 var x = 0; function draw() { clearCanvas(); ctx.fillRect(x, 50, 50, 50); x += 1; requestAnimationFrame(draw); } draw();
2. Interactive drawing techniques
Through the Canvas engine, we can also achieve some interactive drawing effects, such as using the mouse to draw Graphics, dragging graphics, resizing graphics, etc. Here are some practical tips and code examples:
Mouse drawing graphics:
var isDrawing = false; var startX, startY; canvas.addEventListener("mousedown", function (e) { isDrawing = true; startX = e.clientX; startY = e.clientY; }); canvas.addEventListener("mousemove", function (e) { if (isDrawing) { clearCanvas(); var width = e.clientX - startX; var height = e.clientY - startY; ctx.fillRect(startX, startY, width, height); } }); canvas.addEventListener("mouseup", function (e) { isDrawing = false; });
Drag and drop graphics:
var isDragging = false; var offsetX, offsetY; canvas.addEventListener("mousedown", function (e) { var rect = canvas.getBoundingClientRect(); var x = e.clientX - rect.left; var y = e.clientY - rect.top; if (x >= startX && x <= startX + width && y >= startY && y <= startY + height) { isDragging = true; offsetX = x - startX; offsetY = y - startY; } }); canvas.addEventListener("mousemove", function (e) { if (isDragging) { var rect = canvas.getBoundingClientRect(); var x = e.clientX - rect.left; var y = e.clientY - rect.top; startX = x - offsetX; startY = y - offsetY; clearCanvas(); ctx.fillRect(startX, startY, width, height); } }); canvas.addEventListener("mouseup", function (e) { isDragging = false; });
Adjust graphics size:
canvas.addEventListener("mousedown", function (e) { var rect = canvas.getBoundingClientRect(); var x = e.clientX - rect.left; var y = e.clientY - rect.top; if (x >= startX && x <= startX + width && y >= startY && y <= startY + height) { isResizing = true; resizeOffsetX = startX + width - x; resizeOffsetY = startY + height - y; } }); canvas.addEventListener("mousemove", function (e) { if (isResizing) { var rect = canvas.getBoundingClientRect(); var x = e.clientX - rect.left; var y = e.clientY - rect.top; width = x - startX + resizeOffsetX; height = y - startY + resizeOffsetY; clearCanvas(); ctx.fillRect(startX, startY, width, height); } }); canvas.addEventListener("mouseup", function (e) { isResizing = false; });
Conclusion:
The ability to realize interactive drawing through the Canvas engine can add more dynamic effects to our web pages. Give users a better experience. This article introduces some basic drawing and animation implementation and interactive drawing techniques, and provides code samples for developers to refer to. I hope it will be helpful to developers in Canvas drawing, and encourage everyone to further learn and explore more features and usage of the Canvas engine.
The above is the detailed content of Practical method for interactive drawing using Canvas engine. For more information, please follow other related articles on the PHP Chinese website!