简体   繁体   English

在文本框中更改X和Y时,HTML5 Canvas上的重新定位元素

[英]Reposition element on HTML5 Canvas when changing X and Y in textbox

I recently asked this question asking if there was a way to move a layer on the canvas when the user changes the X and Y coördinates in a textbox. 我最近问了一个问题,询问当用户在文本框中更改X和Y坐标时是否可以在画布上移动图层。 I have found a way to do this but it does have some errors in it. 我找到了一种方法来执行此操作,但是它确实存在一些错误。

To make things easier to understand I've added 2 working codepens of my canvas at the bottom of the question! 为了使事情更容易理解,我在问题的底部添加了2个工作正常的画布。

The things I have tried: 我尝试过的事情:

document.getElementById('x').onchange=function(){selectedLayer.offsetX = 100;
drawMarker(selectedLayer);
};

document.getElementById('y').onchange=function(){selectedLayer.offsetY = 100;
drawMarker(selectedLayer);
};

When the user changes the values of the textboxes now the layer will automatically go to the 100.100 coördinates like it should. 现在,当用户更改文本框的值时,图层将自动转到应有的100.100坐标。 Also while using this the layer will keep acting normal after repositioning itself. 同样,在使用此图层时,该图层在重新放置自身后将保持正常行为。 (Still being able to move it) (仍然能够移动它)

The other thing I tried: 我尝试过的另一件事:

document.getElementById('x').onchange=function(){selectedLayer.offsetX = document.getElementById('x').value;
drawMarker(selectedLayer);
};

document.getElementById('y').onchange=function(){selectedLayer.offsetY = document.getElementById('y').value;
drawMarker(selectedLayer);
};

Using this it will get the value of the textbox but there are some problems using this: 使用它会得到文本框的值,但是使用这个会有些问题:
- When typing in a value in the textbox the layer will go to the coördinate which is 100 times bigger then the given value. -在文本框中输入值时,图层将转到坐标,该坐标是给定值的100倍。 (When typing x=1 it will go to x=100) (输入x = 1时,它将转到x = 100)
- After the layer is repositioned I am not able to select/move the layer anymore. -重新定位图层后,我无法再选择/移动图层。

To make things clear here are the 2 examples in working codepens: 为了使事情更清楚,这里是工作代码笔中的两个示例:

The first thing I've tried (offsetX = 100;) 我尝试过的第一件事(offsetX = 100;)

The second thing I've tried (offsetX = document.getElementById('x').value) 我尝试过的第二件事(offsetX = document.getElementById('x')。value)

To try it out in the codepens make a textlayer and move it a bit on the canvas. 要在代码笔中尝试一下,请制作一个文本层并将其在画布上稍微移动一下。 When you see the X and Y filled in at the textboxes at the bottom try changing the values and you will see it happen. 当您在底部的文本框中看到X和Y填充时,请尝试更改值,您会发现它发生了。 (With the second codepen use values below 5) (对于第二个Codepen,请使用低于5的值)

If anyone knows how to fix this so it will get the right value and so it will stay selectable after it would be great! 如果有人知道如何解决此问题,那么它将获得正确的值,因此在它很棒之后将保持可选状态!

Fixed this with a bit of a weird workaround but this is what I'm using now: 通过一些怪异的解决方法解决了此问题,但这是我现在正在使用的方法:

document.getElementById('x').onchange=function(){
    selectedLayer.offsetX = document.getElementById('x').value - canvasOffsetX + 360;
    drawMarker(selectedLayer);
};

document.getElementById('y').onchange=function(){
    selectedLayer.offsetY = document.getElementById('y').value - canvasOffsetY + 101.91;
    drawMarker(selectedLayer);
};

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

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