[英]Incremental Integer in JCanvas text
I'm trying to increment text value everytime I click on the rectangle. 我试图每次单击矩形时增加文本值。 What am I doing wrong ? 我究竟做错了什么 ? I don't understand because the call to my var works in drawText but doesn't in setLayer. 我不明白,因为对我的var的调用在drawText中起作用,但在setLayer中不起作用。 I looked at "setLayer" source code, who's working with '+=' syntax, but text is a String var and I don't want to make String concatenation. 我查看了使用“ + =”语法的“ setLayer”源代码,但是文本是String var,并且我不想进行String串联。
value=1
$('canvas').drawText({
name: 'count',
fillStyle: '#0f0',
x: 20, y: 20,
fontSize: 22,
fontFamily: 'Verdana, sans-serif',
text: value
})
.drawRect({
strokeStyle: '#000',
fillStyle: '#ccc',
x: 20, y: 50,
width: 20,
height: 20,
layer: true,
click: function(layer) {
// Spin
$(this).animateLayer(layer, {
rotate: '+=180'
});
v=parseInt(value);
$(this).setLayer('count',{
text: value+1 // TRYING to increment over here
});
}})
Removing and recreating layers will make jCanvas unhappy. 删除和重新创建图层会使jCanvas不满意。 A much better solution would be to use setLayer()
by parsing the current value to a number, incrementing that value, and passing it to the method: 更好的解决方案是使用setLayer()
是将当前值解析为一个数字,将该值递增,然后将其传递给方法:
$('canvas').drawText({
layer: true,
name: 'count',
fillStyle: '#0f0',
x: 20, y: 20,
fontSize: 22,
fontFamily: 'Verdana, sans-serif',
text: '1'
})
.drawRect({
strokeStyle: '#000',
fillStyle: '#ccc',
x: 20, y: 50,
width: 20,
height: 20,
layer: true,
click: function(layer) {
var $canvas = $(this),
countLayer = $canvas.getLayer('count');
// Spin
$canvas.animateLayer(layer, {
rotate: '+=180'
});
$canvas.setLayer(countLayer,{
text: parseFloat(countLayer.text) + 1
});
}
});
I found a workaround removing the layer and creating a new one at the same place. 我找到了一种解决方法,即删除该层并在同一位置创建一个新层。
function updateCount(param) {
$('canvas').removeLayer("count")
.drawText({
name: 'count',
fillStyle: '#0f0',
x: 250, y: 180,
fontSize: 22,
fontFamily: 'Verdana, sans-serif',
text: value,
layer: true,
maxWidth: 200
})
.drawLayers();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.