|
|

楼主 |
发表于 2022-8-25 13:45
|
显示全部楼层
二楼代码:
- <style>
- #canv { margin: auto; display: block; position: relative; border: 1px solid; }
- </style>
- <canvas id="canv"></canvas>
- <script>
- let ctx = canv.getContext('2d'),
- w = canv.width = 740,
- h = canv.height = 460,
- box = { x: 10, y: 10, width: 60, height: 40, color: 'olive' };
- box.draw = function() {
- ctx.fillStyle = this.color;
- ctx.fillRect(this.x, this.y, this.width, this.height);
- }
- box.draw();
- box.isBox = function(x,y) {
- return (this.x <= x && this.x + this.width >= x && this.y <= y && this.y + this.height >= y);
- }
- canv.addEventListener('mousemove', function(e) {
- let ex = e.offsetX, ey = e.offsetY;
- if (box.isBox(ex,ey)) {
- this.style.cursor = 'pointer';
- box.color = 'red';
- box.draw();
- } else {
- this.style.cursor = 'default';
- box.color = 'olive';
- box.draw();
- }
- });
- </script>
复制代码
|
|