canvas 按钮html5,html - HTML5 canvas at button click? - Stack Overflow

How about “Keeping It Simple”

Create a form that asks all your setup questions.

Put your canvas directly on top of the form and hide it.

When the user has answered their questions: hide the form, show the canvas.

Draw on your canvas.

No need to reinvent any wheels…just HTML.

body{ background-color: ivory; padding:20px; }

#container{position:relative; width:300px; height:300px;}

#setup #canvas{

position:absolute; top:10px; left:10px;

width:100%; height:100%;

}

#setup{padding:10px; border:1px solid blue;}

#canvas{border:1px solid red;}

$(function(){

// Hide the canvas while getting user info on form

$("#canvas").hide();

var canvas=document.getElementById("canvas");

var ctx=canvas.getContext("2d");

function playGame(circles,rects){

// hide the completed form and show the canvas

$("#setup").hide();

$("#canvas").show();

// draw user's circles

ctx.fillStyle="blue";

for(var n=0;n

ctx.save();

ctx.beginPath();

ctx.arc(n*25+15,25,10,0,Math.PI*2,false);

ctx.closePath();

ctx.fill();

ctx.restore();

}

// draw user's rectangles

ctx.fillStyle="green";

for(var n=0;n

ctx.save();

ctx.beginPath();

ctx.rect(n*20+5,75,10,10);

ctx.fill();

ctx.restore();

}

}

$("#play").click(function(){

var circleCount=$("#circles").val();

var rectangleCount=$("#rectangles").val();

playGame( circleCount, rectangleCount );

});

}); // end $(function(){});

How many Circles

How many Rectangles

Play

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值