简体   繁体   English

如何通过在 html5/canvas 中拖动背景来移动对象?

[英]How do you move objects by dragging on the background in html5/canvas?

I would like to be able to create a movable canvas by dragging it, but being able to place objects onto the canvas that will stay in place relative to where they were placed.我希望能够通过拖动它来创建一个可移动的画布,但能够将对象放置在画布上,这些对象将相对于放置的位置保持原位。 How would I go about doing that?我该怎么做?

For example, I place 4 images in different places onto the canvas.例如,我在画布上的不同位置放置了 4 张图像。 Then I drag on an empty space on the canvas and all the objects move based on the movement of the mouse.然后我在画布上的空白处拖动,所有对象都根据鼠标的移动而移动。 Creating an illusion that the canvas has infinite space (because I would like to be able to place several objects beyond what the screen is showing).创建画布具有无限空间的错觉(因为我希望能够在屏幕显示的内容之外放置多个对象)。

Would I have to keep track of the objects themselves, and when I drag an empty space, make all the objects move?我是否必须跟踪对象本身,并在拖动空白区域时让所有对象移动? How will this affect memory if I had say a couple hundred of said objects onto the canvas (being out of view but being able to be brought back into view using this dragging method)?如果我在画布上说了几百个所述对象(不在视野中,但能够使用这种拖动方法重新回到视野中),这将如何影响内存?

The main idea is that I translate the canvas context relative to the mouse movement.主要思想是我相对于鼠标移动翻译画布上下文。 Please read the comments in the code.请阅读代码中的注释。

In this example I'm drawing some circles.在这个例子中,我画了一些圆圈。 You can draw anything else.你可以画任何东西。

 const canvas = document.getElementById("canvas"); const ctx = canvas.getContext("2d"); let cw = canvas.width = window.innerWidth, cx = cw / 2; let ch = canvas.height = window.innerHeight, cy = ch / 2; //the mouse let m = {x:0,y:0};//mouse coords on mouse down let last_m = {x:0,y:0}//mouse coords on dragging //distance let d = {x:0,y:0};// distance while dragging let D = {x:0,y:0};// distance on mouse up // draw some circles let circles = []; let dragging = false; function drawCircle(color){ let o = {} ox = (1 - Math.random() )* 3 * cw; oy = Math.random() * ch; or = (Math.random() * 20) + 10; o.color = color; o.draw = function(){ ctx.fillStyle = o.color; ctx.beginPath(); ctx.arc(ox,oy,or,0,2*Math.PI); ctx.fill(); } circles.push(o) } for(let i = 0; i < 70; i++){ drawCircle(`hsl(${i * 12},80%,60%)`) } for(let i = 0; i < circles.length; i++){circles[i].draw()} //events canvas.addEventListener("mousedown",(evt)=>{ dragging = true; //the mouse position m = oMousePos(canvas, evt); }) canvas.addEventListener("mouseup",(evt)=>{ dragging = false; last_m = oMousePos(canvas, evt); dx = last_m.x - mx; dy = last_m.y - my; // the total dragged distance on mouse up Dx += dx; Dy += dy; }) canvas.addEventListener("mousemove",(evt)=>{ if(dragging){ last_m = oMousePos(canvas, evt); dx = last_m.x - mx + Dx; dy = last_m.y - my + Dy; // clear the context ctx.clearRect(-cw,0, 2*cw,ch); ctx.save(); //translate the context ctx.translate(dx, dy); //repaint everithing for(let i = 0; i < circles.length; i++){circles[i].draw()} ctx.restore() } }) // a function to detect the mouse position on the canvas function oMousePos(canvas, evt) { var ClientRect = canvas.getBoundingClientRect(); return { //objeto x: Math.round(evt.clientX - ClientRect.left), y: Math.round(evt.clientY - ClientRect.top) } }
 *{margin:0;padding:0;} body { overflow: hidden; } canvas { background-color: #000; }
 <canvas id="canvas"></canvas>

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

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