如何在模板中组织画布脚本
how organize canvas script in template
如何将画布脚本分离为助手?目前,它只有在直接写入模板本身的情况下才有效。我想这很琐碎,但我不知道怎么做。谢谢!(scaledPrite也是一个助手)
<template name="spriteBox">
<script>
var myImage = new Image();
myImage.src="{{ spirte.url store='OriginalPix' }}";
myImage.onload = function() {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "{{ sprite.metadata.backColor }}";
ctx.fillRect(0, 0, {{ scaledSprite.widthDevice }}, {{ scaledSprite.heightDevice }});
ctx.imageSmoothingEnabled = false;
ctx.drawImage(myImage, 0, 0, {{ scaledSprite.widthDevice }}, {{ scaledSprite.heightDevice }});
}
</script>
<canvas id="myCanvas" width="{{ scaledSprite.widthDevice }}" height="{{ scaledSprite.heightDevice }}" style="width:{{ scaledSprite.width }}px; height: {{ scaledSprite.height }}px;">
This browser does not support HTML5 canvas
</canvas>
</template>
这不起作用:尝试分离脚本并将其移动到spriteBox.js:
Template.spriteBox.rendered = function () {
var myImage = new Image();
myImage.src= this.sprite.url;
// > Exception from Tracker afterFlush function: Cannot read property 'url' of undefined
// > TypeError: Cannot read property 'url' of undefined
myImage.onload = function() {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = this.sprite.metadata.backColor;
ctx.fillRect(0, 0, scaledSprite.widthDevice, scaledSprite.heightDevice);
ctx.imageSmoothingEnabled = false;
ctx.drawImage(myImage, 0, 0, scaledSprite.widthDevice, scaledSprite.heightDevice);
}
};
HTML
<template name="spriteBox">
<canvas id="myCanvas" width="{{ scaledSprite.widthDevice }}" height="{{ scaledSprite.heightDevice }}" style="width:{{ scaledSprite.width }}px; height: {{ scaledSprite.height }}px;">
This browser does not support HTML5 canvas
</canvas>
</template>
铁路由器
Router.route('/spriteBox/:_id/:boxsize', {
name: 'spriteBox',
template: 'spriteBox',
waitOn: function() {
return [
subs.subscribe('aPix', this.params._id)
];
},
data: function() {
var spriteDocument = MyPix.findOne({_id: this.params._id});
templateData = {
sprite: spriteDocument,
}
return templateData;
}
});
渲染函数的一个明显错误是不能在javascript代码中使用{…}。这只是用于模板。您需要直接调用实际的函数。
基于您的新代码,您有一个新错误:
myImage.src= this.sprite.url;
应该是
myImage.src= this.data.sprite.url;
相关文章:
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 借助asp.net验证或java脚本对多个文本进行验证
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- Java脚本时间添加
- 不显示带有本地json文件数据的谷歌地图脚本
- JQuery添加元素需要在我的js之前再次添加JQuery脚本
- 从远程脚本获取用户IP
- 如何根据时间运行不同的脚本
- 如何将字符串值从php页面发送到java脚本页面
- 使用谷歌应用程序脚本将服务器端数据表返回到客户端
- 可以设置“;文件名"发生错误时显示的内联脚本标记的
- 当脚本由system.js加载时,如何要求('electron')
- HTML标记在脚本标记中工作
- 是否可以控制获取哪些Google地图脚本(JavaScript API)
- 使用谷歌应用程序脚本从工作表中获取值并将其显示在文本框中
- 显示时间的脚本
- ng应用程序使脚本无限运行
- TinyMCE 中没有<样式>标签的脚本脚本
- 获取当前活动的输入字段.Chrome内容脚本.脚本注入
- 脚本脚本需要刷新页面才能运行