1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| <!DOCTYPE html> <html lang="zh_CN">
<head> <title>画布操作(clip)</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content="图形系统开发实战:基础篇 示例"> <meta name="author" content="Jackey Hoo"> <meta name="keywords" content="canvas,anygraph,javascript"> <script src="../js/helper.js"></script> </head>
<body style="margin:10px;"> <canvas id="canvas" width="800" height="533" style="border:solid 1px #CCCCCC;"></canvas> </body> <script> let canvas = document.getElementById('canvas'); let ctx = canvas.getContext('2d'); let beginDrag = false; let ready = false;
let image = new Image(); image.onload = function () { ctx.drawImage(image, 0, 0, canvas.width, canvas.height); drawGrid('lightgray', 0, 0, ctx); } image.src = "../images/bokeh-1123787_640.webp";
canvas.addEventListener('mousedown', function (e) { beginDrag = true; }, false); canvas.addEventListener('mouseup', function (e) { beginDrag = false; }, false); canvas.addEventListener('mousemove', function (e) { if (beginDrag === true) { erase(e.offsetX, e.offsetY, false); } }, false);
function erase(x, y) { ctx.save(); ctx.beginPath(); ctx.arc(x, y, 20, 0, 2 * Math.PI); ctx.clip();
ctx.fillStyle = "#FFFFFF55" ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.restore(); } </script>
</html>
|