- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面是我正在为我的 Phonegap 项目编写的一段代码。
JSFiddle:http://jsfiddle.net/wC79k/
我正在尝试绘制具有提供的 Angular (sensorAngles 数组)的曲线。该数组将通过蓝牙更新(首先作为字符串发送),因此如果接收到的数据没有损坏(也就是每个数组中没有空元素或虚假元素,全长),我将只需要更新曲线。
我现在通过 setTimeout 更新 Angular 数组来“模拟”蓝牙串行更新。
目前,脚本按预期工作(当损坏的数组/字符串时曲线不绘制/更新,并且在收到未损坏的数组后继续更新),但我收到上述错误(未捕获的类型错误)。
这似乎与第 26 行有关,其中损坏的数组使 sensorAngles 未定义(因此 .length 不起作用)。
for (var i = 0; i < (sensorAngles.length); i++){
sensorPos.push({
x: sensorPos[i].x - diffPos(sensorAngles[i]).dx,
y: sensorPos[i].y - diffPos(sensorAngles[i]).dy
});
}
我尝试向其中添加 if 语句,但曲线将不再更新。
我 2 天前才开始使用 Javascript 和 Phonegap,所以任何帮助都会很棒!
干杯,京东
完整代码:
var stage = new Kinetic.Stage({
container: 'myCanvas',
width: window.innerWidth,
height: window.innerHeight
});
var sensorLayer = new Kinetic.Layer();
/* initialise an empty array of all the angles */
var sensorAngles = [0, 0, 0, 0, 0, 0, 0, 0];
//var sensorPos = [];
/* divide each sensor distance segment by height of screen/window for
maximum amount of canvas being used */
var sensorDistance = (stage.getHeight() / ((sensorAngles.length) + 2));
function diffPos(angle){ // angles in radians
return {
dx: sensorDistance * Math.sin(angle),
dy: sensorDistance * Math.cos(angle)
}
};
function calcSpline(sensorAngles){
sensorPos = [{x: (stage.getWidth() / 2), y: (stage.getHeight() - sensorDistance)}];
for (var i = 0; i < (sensorAngles.length); i++){
sensorPos.push({
x: sensorPos[i].x - diffPos(sensorAngles[i]).dx,
y: sensorPos[i].y - diffPos(sensorAngles[i]).dy
});
}
return sensorPos;
};
calcSpline(sensorAngles);
var sensorPos = calcSpline(sensorAngles);
var spine = new Kinetic.Spline({
points: sensorPos,
stroke: 'red',
strokeWidth: 2,
lineCap: 'round',
tension: 0.3
});
sensorLayer.add(spine);
stage.add(sensorLayer);
function updateSpline(angles){
calcSpline(sanityCheck(angles));
spine.setPoints(sensorPos);
sensorLayer.draw();
};
function sanityCheck(data) {
data = data.split(",");
if (data.filter(function(n){return n}).length === sensorAngles.length) {
sensorAngles = data;
return sensorAngles;
}
}
setTimeout(
function() {
updateSpline("0.1,0.12,0.08,-0.2,0.1,0.85,0.1,-0.2")
},1000
);
setTimeout(
function() {
updateSpline("0.1,0.2,0.1,-0.1,0.1,0.1,0.1,-0.2")
},2000
);
setTimeout(
function() {
updateSpline("0.1,0.12,0.08,-0.2,0.1,0.85,0.1,-0.2")
},3000
);
setTimeout(
function() {
updateSpline("0.4,0.5,,-0.6,0.2,0.96,0.02,-0.01") //corrupt
},4000
);
setTimeout(
function() {
updateSpline("0.1,,0.08,-0.2,0.1,0.85,0.1,-0.2") // corrupt
},5000
);
setTimeout(
function() {
updateSpline("0.6,0.12,0.22,-0.2,0.1,0.85,0.1,-0.2")
},6000
);
最佳答案
如果没有得到长度,你可以尝试 console.log()并检查您是否在控制台中获得写入值?
示例
console.log()
for (var i = 0; i < (sensorAngles.length); i++){
sensorPos.push({
x: sensorPos[i].x - diffPos(sensorAngles[i]).dx,
y: sensorPos[i].y - diffPos(sensorAngles[i]).dy
});
}
关于javascript - 未捕获的类型错误 : Cannot read property 'length' of undefined (array),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17841287/
我找到了以下代码片段: length = length and length or len(string) 在我看来,这应该等同于: length = length or len(string) 我能
当我使用 numpy.shape() 检查数组的形状时,我有时会得到 (length,1) 有时会得到 (length,)。看起来区别在于列向量与行向量......但它似乎并没有改变数组本身的任何内容
我正在学习 Java,有一个简单的问题。 在设置类的示例中,我看到了这一点: length >= 0 ? length : length * -1 这是什么意思? 谢谢。 最佳答案 这是一种骇人听闻的
我在阅读有关在 Ruby 中重新定义方法有多么容易的文章时遇到了以下问题: class Array alias :old_length :length def length old_l
例如在下面的代码中a和b和c是相等的。 EditText editText; editText = (EditText) findViewById(R.id.edttxt); editText.set
在昨天教授我的 JavaScript 类(class)时,我和我的学生遇到了一些有趣的功能,我认为这些功能可能值得在一个问题和我得出的答案中捕捉到。 在 Chrome 的 JS 控制台中输入 Arra
这个问题在这里已经有了答案: How can I get the size of an array, a Collection, or a String in Java? (3 个回答) 3年前关闭。
这个问题在这里已经有了答案: length and length() in Java (8 个答案) 关闭 6 年前。 我注意到在计算数组的长度时,你会这样写: arrayone.length; 但
console.log(this.slides.length()); 打印 Cannot read property 'length' of undefined.在 setTimeout 为 100
在搜索stackoverflow问题时,我发现了此链接: Error in file.download when downloading custom file。 但是,我的情况有些不同(我认为):
这个问题已经有答案了: Why does R use partial matching? (1 个回答) 已关闭 8 年前。 大家。我刚刚开始使用 swirl 学习 R 编程。 我刚刚了解到seq 。
这个问题已经有答案了: Why does R use partial matching? (1 个回答) 已关闭 8 年前。 大家。我刚刚开始使用 swirl 学习 R 编程。 我刚刚了解到seq 。
这个问题已经有答案了: How can I get the size of an array, a Collection, or a String in Java? (3 个回答) 已关闭 9 年前。
我有一个大数组,其中包含所有类型( bool 值,数组,null,...),并且我正在尝试访问它们的属性arr[i].length,但有些其中显然没有长度。 我不介意那些缺少长度的人是否返回未定义(我
我在对象的属性中有一些文本。我正在测试对象的属性中是否有要显示的文本;如果没有,那么我显示“-”而不是空白。看起来没有什么区别: if (MyObject.SomeText && MyObject.S
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Why is String.length() a method? Java - Array's length
这个问题在这里已经有了答案: obj.length === +obj.length in javascript (4 个答案) 关闭 9 年前。 我一直在读underscore.js源代码并在 _.
#include using std::cout; using std::cin; using std::string; int main(){ cout > name; cout
我正在细读 underscore.js annotated source当我遇到这个时: if (obj.length === +obj.length) {...} 我现在从this stackove
我正在查看 dotnet 运行时中的一些代码,我注意到不是这样写的: if (args.Length > 0) 他们使用这个: if (args is { Length: > 0}) 你知道用第二种方
我是一名优秀的程序员,十分优秀!