[英]Cannot draw into HTML Canvas from Jquery plugin
我正在創建我的第一個Jquery插件。 我無法在使用它的畫布上繪畫。
(function($){
$.fn.graph=function(options){
var settings=$.extend({
color:'orange'
},options);
this.css({backgroundColor:settings.color});
this.element=document.createElement('canvas');
var ctx=this.element.getContext("2d");
ctx.font = "30px Arial";
ctx.fillText("Hello World", 10, 50);
console.log(this.element);
};
}(jQuery));
您的element
需要附加到頁面上。 采用:
$( this.element ).appendTo( 'body' )
來自.appendTo()
。
這是最終代碼:
( function( $ ) { $.fn.graph = function( options ) { var settings = $.extend({ color: 'orange' }, options ); this.css({ backgroundColor: settings.color }); this.element = document.createElement( 'canvas' ); $( this.element ).appendTo( 'body' ); var ctx = this.element.getContext('2d'); ctx.font = '30px Arial'; ctx.fillText('Hello World', 10, 50); console.log( this.element ); }; }( jQuery ) ); $( 'body' ).graph();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.