亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

context在這里是全局變量吧,那么是不是在draw函數中就可以直接使用context來進行繪制了,為什么還要在把context的值傳給cxt呢?求指點

$("#tangram").bind("click",function tangram() {
? ?var canvas = document.getElementById("canvas");
? ? canvas.width = 800;
? ? canvas.height = 700;
? ?if (canvas.getContext("2d")) {
? ? ? ?var context = canvas.getContext("2d");
? ?}
? ?else {
? ? ? ?alert("當前瀏覽器不支持Canvas,推薦使用Chrome瀏覽器")
? ?}
? ?var tangram=[
? ? ? ?{p:[{x:0,y:0},{x:800,y:0},{x:400,y:400}],color:"red"},
? ? ? ?{p:[{x:0,y:0},{x:400,y:400},{x:0,y:800}],color:"yellow"},
? ? ? ?{p:[{x:0,y:0},{x:800,y:0},{x:400,y:400}],color:"red"},
? ? ? ?{p:[{x:0,y:0},{x:800,y:0},{x:400,y:400}],color:"red"},
? ? ? ?{p:[{x:0,y:0},{x:800,y:0},{x:400,y:400}],color:"red"},
? ? ? ?{p:[{x:0,y:0},{x:800,y:0},{x:400,y:400}],color:"red"},
? ? ? ?{p:[{x:0,y:0},{x:800,y:0},{x:400,y:400}],color:"red"},
? ?]
? ?for (var i=0;i<tangram.length;i++){
? ? ? ?draw(tangram[i]);
? ?}
? ?function draw(piece){
? ? ? ?context.beginPath();
? ? ? ?context.moveTo(piece.p[0].x,piece.p[0].y);
? ? ? ?for (var i=1;i<piece.length;i++){
? ? ? ? ? ?context.lineTo(piece[i].x,piece[i].y);
? ? ? ?}
? ? ? ?context.closePath();
? ? ? ?context.fillStyle=tangram[0].color;
? ? ? ?context.fill();
? ?}

正在回答

1 回答

我的理解是對的,之所以沒畫出來,我的代碼錯在這里了:

for (var i=1;i<piece.length;i++){
? ? ? ? ? ?context.lineTo(piece[i].x,piece[i].y);
? ? ? ?}

應該是

i<piece.p.length

context.lineTo(piece.p[i].x,piece.p[i].y)



1 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

context在這里是全局變量吧,那么是不是在draw函數中就可以直接使用context來進行繪制了,為什么還要在把context的值傳給cxt呢?求指點

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號