admin管理员组文章数量:1310315
I have written a simple template for a AngularJs directive:
<div class="circle">
<canvas></canvas>
</div>
My directive has the following look:
MobileApp.directive('circle', function () {
return {
restrict: 'E',
scope: true,
link: function (scope, elem, attrs) {
...
var canvas = elem.find('canvas');
var ctx = canvas.getContext('2d');
...
}
}
});
And the usage of that is simple enough:
<circle/>
I am getting an error when trying to call a method getContext('2d')
:
TypeError:
canvas.getContext
is not a function
The canvas
instance is selected and its JSON representation looks
{
"0": {},
"length": 1,
"prevObject": {
"0": {},
"context": {},
"length": 1
},
"context": {},
"selector": "canvas"
}
I have already read about all related questions here, but the answer hasn't been found. Any help would be appreciated. Thanks.
I have written a simple template for a AngularJs directive:
<div class="circle">
<canvas></canvas>
</div>
My directive has the following look:
MobileApp.directive('circle', function () {
return {
restrict: 'E',
scope: true,
link: function (scope, elem, attrs) {
...
var canvas = elem.find('canvas');
var ctx = canvas.getContext('2d');
...
}
}
});
And the usage of that is simple enough:
<circle/>
I am getting an error when trying to call a method getContext('2d')
:
TypeError:
canvas.getContext
is not a function
The canvas
instance is selected and its JSON representation looks
{
"0": {},
"length": 1,
"prevObject": {
"0": {},
"context": {},
"length": 1
},
"context": {},
"selector": "canvas"
}
I have already read about all related questions here, but the answer hasn't been found. Any help would be appreciated. Thanks.
Share Improve this question edited Jun 23, 2016 at 14:11 Andrew asked Jun 23, 2016 at 13:32 AndrewAndrew 49.7k15 gold badges98 silver badges144 bronze badges 02 Answers
Reset to default 6I create a little plunker. In your sample i don't understand the relation between directive and template. Have you missing to add template property ? What i do in plunker :
Define template :
return {
restrict: 'E',
scope: true,
template: '<div><canvas></canvas></div>',
link: function (scope, elem, attrs) {
var canvas = elem.find('canvas');
console.log(canvas);
var ctx = canvas[0].getContext('2d');
console.log(canvas, ctx);
}
}
Get canvas from the object that return by find method :
var ctx = canvas[0].getContext('2d');
And the use :
<circle></circle>
Replace var canvas = elem.find('canvas');
by var canvas = elem.find('canvas')[0];
本文标签: javascriptcanvasgetContext is not a functionStack Overflow
版权声明:本文标题:javascript - canvas.getContext is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741819970a2399305.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论