【问题标题】:Is there a way of drawing an image OVER HTML5 canvas 2d drawings?有没有办法在 HTML5 画布 2d 图纸上绘制图像?
【发布时间】:2016-05-07 10:47:12
【问题描述】:

每次我用谷歌搜索这个,它都会给我反向解决方案的答案——在图像上绘制东西。我知道该怎么做。

我的问题是我想在一些画布上绘制我的图像。无论我做什么,似乎绘图总是与图像重叠。

问题可能是我在动画循环中执行此操作,并且不知何故没有绘制图像?

你可以看到我的代码here。 (只是很短)。

如您所见,在我的绘图函数中,我有在绘制其他内容之后绘制的图像。我通过注释掉代码检查了图像是否正确绘制,嘿,图像出现了。 drawImage代码如下:

var drawCanvasImage = new Image();
drawCanvasImage.src = "https://www.w3.org/html/logo/downloads/HTML5_Logo_256.png";
drawCanvasImage.onload = function(){ 
  ctx.drawImage(drawCanvasImage, 0, 0); 
}

谁能提供对此的见解?谢谢。

【问题讨论】:

标签: javascript html canvas html5-canvas


【解决方案1】:

你应该这样做:

  1. 加载所有必要的资源。图像、音频等。

    如果需要,您可以显示“正在加载”消息。

  2. 开始绘画/复制/随便什么。

    既然有资源,可以直接使用。

    在此处避免使用 load 事件处理程序。

var drawCanvasImage = new Image();
drawCanvasImage.onload = function(){
  animFrame(recursiveAnim);
};
drawCanvasImage.src = "myimage";
function drawScreen() {
  // ...
  ctx.drawImage(drawCanvasImage, 0, 0); 
}

var animFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame    || window.oRequestAnimationFrame      || window.msRequestAnimationFrame     || null ;

window.addEventListener('mousemove', saveMove, false);

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

//Global vars
var clickX, clickY;

//Loops through to draw the graphics
mainLoop = function() {
  drawScreen();
};

//This loops the animation frames
var recursiveAnim = function() {
  mainLoop();
  animFrame(recursiveAnim);//
};
var drawCanvasImage = new Image();
drawCanvasImage.onload = function(){
  animFrame(recursiveAnim);
};
drawCanvasImage.src = "https://www.w3.org/html/logo/downloads/HTML5_Logo_256.png";


function drawScreen() {
  //sky
  ctx.fillStyle="#33ccff";
  ctx.fillRect(0, 0, c.width, c.height);
  //sun
  ctx.beginPath();
  ctx.arc(680, 150, 90, 10, Math.PI, true);
  ctx.closePath();
  ctx.lineWidth = 5;
  ctx.fillStyle = '#ffff33';
  ctx.fill();
  ctx.strokeStyle = '#b2b300';
  ctx.stroke();  
  //grass
  ctx.beginPath();
  ctx.arc(350, 950, 800, 0, Math.PI, true);
  ctx.closePath();
  ctx.lineWidth = 5;
  ctx.fillStyle = '#3fff00';
  ctx.fill();
  ctx.strokeStyle = '#1f8000';
  ctx.stroke();

  ctx.drawImage(drawCanvasImage, 0, 0); 

  //Hey SO, thanks for checking this out! :)
}


// Mouse click stuff #############################################
{
  function saveMove(e) {
    var pos = getMousePos(c, e);
    clickX = pos.x;
    clickY = pos.y;
  }

  function getMousePos(c, evt) {
    var rect = c.getBoundingClientRect();
    return {
      x: evt.clientX - rect.left,
      y: evt.clientY - rect.top
    }
  }  
}
.title {
  font-family: "Helvetica", sans-serif;
}
<canvas id="myCanvas" width="800" height="300" style="border:1px solid #000000"></canvas>

【讨论】:

  • 感谢您对此的回答。这看起来很棒,但是如果我想加载多个图像,我不确定如何进行。我可以看到您在说“加载图像后,开始动画。”。虽然我可以想象从上一个图像加载中调用加载下一个图像是可能的(基本上是通过整个列表加载),但有没有更好的方法来做到这一点?谢谢你的帮助! :)
  • 我想我已经解决了。我基本上只是对代码中列出的最后一个图像进行了加载,然后将它们全部加载。感谢您的帮助:~)
  • @CameronC 当您有多个资源时,您可以按顺序加载它们(一旦加载了一个,就开始加载下一个),或者您可以同时开始加载它们,当每个资源都加载时减少一个计数器(当它达到零时,调用主代码)。由于异步性,管理这类事情可能很乏味,但一些库或 ES6 承诺可以提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
  • 2014-05-17
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
  • 2018-10-20
相关资源
最近更新 更多