【问题标题】:How can I make a Coordinates Object Dynamic in JavaScript?如何在 JavaScript 中使坐标对象动态化?
【发布时间】:2013-08-26 01:54:51
【问题描述】:

我正在使用 Titanium Appcelerator 的一个模块来绘制线条。在线条中,您只需要 2 个带有坐标的对象即可绘制线条(起点和终点)。

我想用“touchmove”事件修改开始/结束点对象,但是我对 JavaScript 还很陌生,只是想不出正确的方法。

var lines = require('com.lines'); //<< Import lines module

var attrib = { 
    color:'red',
    touchEnable:true,
    thickness:20
};

var from = {x:20,y:100}; //<< Start Point Object Coordinates
var to = {x:200,y:100}; //<< End POint Object Coordinates
var lineOne = lines.createLine(from, to, attrib); //<<Creates lineOne var with attributes
win.add(lineOne); //Draw the line. 

使用 lineOne 变量移动整行没有问题,但是正如我所说,我找不到动态修改对象的方法。

谢谢!

更新 - 在 Josiah 的建议之后,我能够成功修改“to”变量,但是无论如何,该行都会用原始的“to”值重新绘制自己。我要么错误地使用了 setTo 方法,要么我需要其他东西来更新和重绘线..

var attrib = {
    color:'red',
    touchEnable:true,
    thickness:20
};

var from = {x:20,y:100}; //Insert touchMoveFunction <<-- Here! 
var to = {x:200,y:100};
var lineOne = lines.createLine(from, to, attrib);

function drawline(e){
    win.add(lineOne);
};

alert(to);

var lineOne = lines.createLine(from,to,attrib);

var buttonPosition = Ti.UI.createButton({
    title:'CLick.Me!',
    bottom:0
});
win.add(buttonPosition);

buttonPosition.addEventListener('click',function(e){
    to = {x:200,y:300};
    lineOne.setTo(to);
    drawline();
    alert(to);
});

【问题讨论】:

    标签: javascript titanium titanium-mobile titanium-modules


    【解决方案1】:

    只需将touchmove 的事件侦听器附加到窗口(或任何包含线条的视图容器),然后刷新,或任何必要的线条更改,这里有一个小例子:

    var lineOne = lines.createLine(from, to, attrib);
    win.add(lineOne);
    win.addEventListener('touchmove', function(e) {
        win.remove(lineOne);
        var touchX = e.x;
        var touchY = e.y;
        var to = {x:touchX,y:touchY}; 
        lineOne = lines.createLine(from, to, attrib);
        // Add a new line
        win.add(lineOne);
    });
    

    【讨论】:

    • 嘿,谢谢 Josiah,我在更新“to”变量时遇到了麻烦,显然我要么错误地使用了 setTo 方法,要么钛中没有 setTo 方法。我使用了几个警报来验证“to”变量是否正在更新,并且它正在工作。问题是这条线无论如何都会用原来的“to”位置重新绘制。
    • 我从来没有使用过lines模块,所以我在猜测你必须做什么。试试上面的方法,把旧线去掉,然后重画。
    猜你喜欢
    • 2023-03-14
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 2018-12-08
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    相关资源
    最近更新 更多