在保留图像的同时在HTML5 Canvas中绘制图像

时间:2013-11-13 23:57:42

标签: javascript html5 canvas drawing

在HTML5 Canvas中,最简单的方法是在图像上绘制并移动一条线(已在画布上),保留下面的图像? (例如,有一条垂直线跟踪鼠标X的位置)

我目前的画布:

$(document).ready(function() {
  canvas = document.getElementById("myCanvas");
  context = canvas.getContext("2d");
  imageObj = new Image();

    imageObj.onload = function() { 
    context.drawImage(imageObj, 0,0);  
  }
  imageObj.src = "http://example.com/some_image.png";
  $('#myCanvas').click(doSomething);
});

P.S。我正在与你的Facebook网络竞争。愿最好的Lazyweb获胜(我的感激之情):)

4 个答案:

答案 0 :(得分:15)

你必须使用canvas进行大部分的基础工作,在这种情况下,你必须实现移动线然后重绘所有内容的功能。

步骤可以是:

  • 将该行保留为可以自我渲染的对象(对象上的方法)
  • 收听mousemove(在本例中)以移动行
  • 对于每次移动,重绘背景(图像)然后将线条渲染到新位置

您可以整体重绘背景,也可以将其优化为仅绘制最后一行。

以下是一些示例代码和 live demo here

var canvas = document.getElementById('demo'), /// canvas element
    ctx = canvas.getContext('2d'),            /// context
    line = new Line(ctx),                     /// our custom line object
    img = new Image;                          /// the image for bg

ctx.strokeStyle = '#fff';                     /// white line for demo

/// start image loading, when done draw and setup 
img.onload = start;
img.src = 'http://i.imgur.com/O712qpO.jpg';

function start() {
    /// initial draw of image
    ctx.drawImage(img, 0, 0, demo.width, demo.height);

    /// listen to mouse move (or use jQuery on('mousemove') instead)
    canvas.onmousemove = updateLine;
}

现在我们需要做的就是有机会来更新每一步的背景和线条:

/// updates the line on each mouse move    
function updateLine(e) {

    /// correct mouse position so it's relative to canvas
    var r = canvas.getBoundingClientRect(),
        x = e.clientX - r.left,
        y = e.clientY - r.top;

    /// draw background image to clear previous line
    ctx.drawImage(img, 0, 0, canvas.width, canvas.height);

    /// update line object and draw it
    line.x1 = x;
    line.y1 = 0;
    line.x2 = x;
    line.y2 = canvas.height;
    line.draw();
}

自定义行对象在此演示中非常简单:

/// This lets us define a custom line object which self-draws
function Line(ctx) {

    var me = this;

    this.x1 = 0;
    this.x2 = 0;
    this.y1 = 0;
    this.y2 = 0;

    /// call this method to update line        
    this.draw = function() {
        ctx.beginPath();
        ctx.moveTo(me.x1, me.y1);
        ctx.lineTo(me.x2, me.y2);
        ctx.stroke();
    }
}

如果您不打算对图像本身做任何特定的事情,您也可以使用CSS将其设置为背景图像。在重新绘制线之前,您仍然需要清除画布。

答案 1 :(得分:3)

您可以通过收听mousemove事件获得“十字准线”,然后:

  • 清除画布
  • 绘制图像
  • 在鼠标位置绘制线条

这是代码和小提琴:http://jsfiddle.net/m1erickson/jEc7N/

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<style>
    body{ background-color: ivory; padding:20px; }
    #canvas{border:1px solid red;}
</style>

<script>
$(function(){

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");
    ctx.lineWidth=2;

    var canvasOffset=$("#canvas").offset();
    var offsetX=canvasOffset.left;
    var offsetY=canvasOffset.top;

    var img=new Image();
    img.onload=function(){
        canvas.width=img.width;
        canvas.height=img.height;
        ctx.drawImage(img,0,0);

    }
    img.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/KoolAidMan.png";

    function handleMouseMove(e){
      mouseX=parseInt(e.clientX-offsetX);
      mouseY=parseInt(e.clientY-offsetY);

      ctx.clearRect(0,0,canvas.width,canvas.height);
      ctx.drawImage(img,0,0);
      ctx.beginPath();
      ctx.moveTo(mouseX,0);
      ctx.lineTo(mouseX,canvas.height);
      ctx.moveTo(0,mouseY);
      ctx.lineTo(canvas.width,mouseY);
      ctx.stroke();


    }

    $("#canvas").mousemove(function(e){handleMouseMove(e);});


}); // end $(function(){});
</script>

</head>

<body>
    <canvas id="canvas" width=300 height=300></canvas>
</body>
</html>

答案 2 :(得分:3)

这可能不是一个真正的答案,以防你需要它(将来)。使用某些库,使用canvas会更好(也更容易)。我尝试过CreateJS的EaselJS,发现自己很喜欢它。 你可以看一下它EaselJS (我已经做了一个例子,允许很久以前使用EaselJS绘制和拖动图像)

答案 3 :(得分:3)

或只使用 2层

  1. 背景图层有图片但不会改变,
  2. 顶层有线,你可以清除并重绘大量时间而不影响背景层。
相关问题