如何用图像填充画布

Rmanx77

我创建了一个画布,例如有4个单元格,如何为每个单元格设置图像?我的Canvas旋转了,我也不想图像也旋转,当鼠标在单元格上移动时,图像也消失了!

我的代码:

<html>
<head>
<script src="jquery-3.4.1.js"></script>
</head>
<body bgcolor="black">
<canvas id="tutorial" width="500" height="500"></canvas>
<script type="text/javascript">
var canvas = document.querySelector("canvas"),
    ctx = canvas.getContext("2d"),
    rects = [
        {x: 0, y: 0, w: 100, h: 100},
        {x: 0, y: 100, w: 100, h: 100},
        {x: 100, y: 100, w: 100, h: 100},
        {x: 100, y: 0, w: 100, h: 100}
    ], i = 0, r;
    ctx.translate(canvas.width / 2, canvas.height / 2);
    ctx.scale(0.71, 0.3834);
    ctx.rotate(-0.25 * Math.PI);
// render initial rects.
while(r = rects[i++]) ctx.rect(r.x, r.y, r.w, r.h);
// var img = new Image();
// img.onload = function () {
    // ctx.drawImage(img, 0, 0);
// }
// img.src = "i1.png";
ctx.fillStyle = "#c5de89"; 
ctx.fill();
canvas.onmousemove = function(e) {
  var rect = this.getBoundingClientRect(),
      x = e.clientX - rect.left,
      y = e.clientY - rect.top,
      i = 0, r;
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  while(r = rects[i++]) {
    ctx.beginPath();
    ctx.rect(r.x, r.y, r.w, r.h);    
    ctx.fillStyle = ctx.isPointInPath(x, y) ?  "red" : "#c5de89";
    ctx.fill();
  }

};
</script>
</body>
</html>

我想要这样的东西:

在此处输入图片说明

我的画布:

在此处输入图片说明

里沙卜

您还应该在onmousemove函数中添加图像x和y坐标,因为您正在该函数中重建画布。

var angle = 0;
var canvas = document.getElementById("tutorial"),
  ctx = canvas.getContext("2d"),
  rects = [{
      x: 0,
      y: 0,
      w: 100,
      h: 100
    },
    {
      x: 0,
      y: 100,
      w: 100,
      h: 100
    },
    {
      x: 100,
      y: 100,
      w: 100,
      h: 100
    },
    {
      x: 100,
      y: 0,
      w: 100,
      h: 100
    }
  ],
  i = 0,
  r;
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.scale(0.71, 0.3834);
ctx.rotate(-0.25 * Math.PI);
// render initial rects.




while (r = rects[i++]) {
  ctx.rect(r.x, r.y, r.w, r.h);
  ctx.fillStyle = "#c5de89";
  ctx.fill();
  ctx.save();
  ctx.rotate(0.25 * Math.PI);

  base_image = new Image();
  base_image.src = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';
  ctx.drawImage(base_image, r.x, r.y - 75);
  ctx.restore()
}



canvas.onmousemove = function(e) {
  var rect = this.getBoundingClientRect(),
    x = e.clientX - rect.left,
    y = e.clientY - rect.top,
    i = 0,
    r;
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  while (r = rects[i++]) {
    ctx.beginPath();
    ctx.rect(r.x, r.y, r.w, r.h);
    ctx.fillStyle = ctx.isPointInPath(x, y) ? "red" : "#c5de89";
    ctx.fill();
    ctx.save();
    ctx.rotate(0.25 * Math.PI);

    base_image = new Image();
    base_image.src = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';
    ctx.drawImage(base_image, r.x, r.y - 100);
    ctx.restore()
  }


};
<html>

<head>
  <script src="jquery-3.4.1.js"></script>
</head>

<body bgcolor="black">
  <canvas id="tutorial" width="500" height="500"></canvas>
</body>

</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何用奇异果中的图像填充画布?

来自分类Dev

如何用不同的图像填充画布圆弧?

来自分类Dev

如何用选择的颜色填充画布?

来自分类Dev

如何用图像填充路径?

来自分类Dev

如何用图案填充图像

来自分类Dev

如何用图像填充Silvertripe 3.1 Gridfield

来自分类Dev

如何用css填充png图像

来自分类Dev

如何用 HTML 中的图像填充文本?

来自分类Dev

如何填充一个以上画布图像的颜色?

来自分类Dev

如何用工作表中的形状填充图像列表?

来自分类Dev

如何用css和html用图像填充圆?

来自分类Dev

如何用图像填充P5.js形状

来自分类Dev

如何用图像OpenCV填充多边形

来自分类Dev

上传的图像填充画布Javascript

来自分类Dev

如何用箭头键在画布html5上移动图像?

来自分类Dev

如何用箭头键在画布html5上移动图像?

来自分类Dev

如何替换画布图像?

来自分类Dev

用图像填充Tkinter画布元素

来自分类Dev

如何在画布上使用Canvas在移动对象上用自定义图像填充对象?

来自分类Dev

如何用渐变填充圆?

来自分类Dev

如何用鼠标在画布上绘制箭头

来自分类Dev

如何用流星创建全窗口画布?

来自分类Dev

如何用画布摆脱饼图中的暗线?

来自分类Dev

如何用图像和文本填充一个数组值?-HTML / Javascript

来自分类Dev

如何用透明背景和填充物覆盖轮廓图像?

来自分类Dev

如何用进度条填充透明图像?#CSS #HTML #Jquery?

来自分类Dev

如何用画布PaperJS绘制平行线?(画布/ JavaScript)

来自分类Dev

如何更新画布上的图像?

来自分类Dev

如何从画布上拖动图像?