简体   繁体   English

跟随画布光标的放大镜

[英]Magnifying glass that follows cursor for canvas

I'm working on a t-shirt designer for a client of mine and I made it using html5 canvas.我正在为我的一个客户设计 T 恤设计师,我使用 html5 画布制作了它。 The shirt designer is now finished but he requested I add a magnifying glass (something like this: http://mlens.musings.it/ ).衬衫设计师现在已经完成了,但他要求我添加一个放大镜(类似这样: http : //mlens.musings.it/ )。 I've found a lot of similar scripts out there, however, they all seem to have one thing in common they use a small and a large image.我在那里发现了很多类似的脚本,但是,它们似乎都有一个共同点,它们使用小图像和大图像。 Using canvas, however, I have a different scenario and have been stumped on how I can add a magnifying glass to magnify where ever the cursor is on canvas.然而,使用画布,我有一个不同的场景,并且一直困扰着我如何添加一个放大镜来放大光标在画布上的任何地方。

Are there any scripts that exist that can get this done?是否有任何脚本可以完成此任务? or what other options would I have?或者我还有什么其他选择?

Let's just draw the canvas to another canvas but scaled.让我们将画布绘制到另一个画布上但进行缩放。

fiddle example小提琴示例

context.drawImage allows us to specify how much of the origin canvas we want to get and how big to draw it on the destination canvas. context.drawImage允许我们指定我们想要获得多少原始画布以及在目标画布上绘制它的大小。 So to do a times 2 zoom we just draw the origin canvas twice the size to the destination canvas.因此,要进行 2 倍缩放,我们只需将两倍大小的原始画布绘制到目标画布。

var main = document.getElementById("main");
var zoom = document.getElementById("zoom");
var ctx = main.getContext("2d")
var zoomCtx = zoom.getContext("2d");
var img = new Image();
img.src = "http://astrobioloblog.files.wordpress.com/2011/10/duck-1.jpg"
img.onload = run;

function run(){
    ctx.drawImage(img,0,0);
}

main.addEventListener("mousemove", function(e){
    //console.log(e);
    zoomCtx.drawImage(main, e.x, e.y, 200, 100, 0,0, 400, 200);
    zoom.style.top = e.pageY + 10+ "px"
    zoom.style.left = e.pageX +10+ "px"
    zoom.style.display = "block";
});

main.addEventListener("mouseout", function(){
    zoom.style.display = "none";
});

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

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