简体   繁体   English

如何在画布上同时绘制图像和素描

[英]how to draw both image and sketch on canvas together

I want to draw an image to canvas and then allow user to draw some sketch on it by sketch.js. 我想在画布上绘制图像,然后允许用户通过sketch.js在其上绘制一些草图。 Now the situation is: 现在的情况是:

  1.the image has been drawn on the canvas successfully
  2. but when my mouse over the canvas, then image disappeared, and the canvas shows empty
  3. when I dragged the mouse, some sketch shows on the empty canvas(the sketch looks correct)

so, I've made both functions right, but I'm confused about the part in between. 因此,我把两个功能都做对了,但是我对两者之间的部分感到困惑。 I hope to draw the sketch on the canvas with the image on it. 我希望将素描画在画布上并在上面画上图像。 Here is my code: 这是我的代码:

index.html: index.html的:

<canvas id="mysketch" width="578" height="400" style="margin: 0px; "></canvas>

canvas.js: canvas.js:

var mysketch = jQuery("#mysketch");

// draw the captured image to canvas 
    var displayImage = function() {
    var context = mysketch.get(0).getContext("2d");     
        var image = new Image();

        image.onload = function() {
            context.drawImage(image, 0, 0);
        }

    // prepend the required image info again
        image.src = dataURL;

        // call sketch.js to draw sketch on the canvas
    mysketch.sketch({defaultColor: "#ff0"});

    }

I think that, on your call to sketch method 我认为,在您要求草绘方法时
sketch.js is clearing the canvas first then allows you to draw something on it. sketch.js首先清除画布,然后允许您在其上绘制一些内容。
As you can see in the link here , in the 3rd example (ie Tools Example) the image is not drawn by drawImage method. 正如您在此处的链接中看到的那样,在第三个示例(即“工具示例”)中,图像不是通过drawImage方法绘制的。
It is actually the background of the canvas which will always be there as background, whatever you draw on it. 实际上,它是画布的背景,无论您在其上绘制什么,它都将始终作为背景。

So what you can do is: 因此,您可以做的是:

either do same thing as in the example ie set your image as background, 可以执行与示例相同的操作,即将您的图片设置为背景,

or make a new canvas just behind your canvas with same width, height and on same location and draw your image on it. 或在画布后面以相同的宽度,高度和相同的位置制作新的画布,并在其上绘制图像。 and do your sketch thing on the second one. 然后在第二个上做草图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM