html5 challenge,html - HTML5 Canvas Challenge - Stack Overflow

In the first few lines, you need to parseInt from each of the .val() functions. So:

var number = $("#number option:selected").val();

var shape = $("#shape option:selected").val();

var size = $("#size option:selected").val();

becomes

var number = parseInt($("#number option:selected").val());

var shape = $("#shape option:selected").val();

var size = parseInt($("#size option:selected").val());

but the size and "offset" calculations are all done in the wrong place. they need to be done in the main loop while the drawShape methods each have the task of drawing a given shape in a given location of a specified size. http://jsfiddle.net/3uyLc/39/

Here's the fixed code:

jQuery.noConflict();

(function($) {

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

console.log("clear!");

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

var context=c.getContext("2d");

context.clearRect(0, 0, canvas.width, canvas.height);

});

function square(offset, size){

var color = $("#color option:selected").val();

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

var context=c.getContext("2d");

context.fillStyle = color;

context.fillRect(offset,0,size,size);

}

function circle(offset, size){

var color = $("#color option:selected").val();

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

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

var radius = size / 2;

var x = offset + radius;

var y = radius;

context.beginPath();

context.arc(x, y, radius, 0, 2 * Math.PI, false);

context.lineWidth = 1;

context.fillStyle = color;

context.fill();

//context.fillStyle="#ff0000";

//context.fillRect(x-1, y-1, 2, 2);

}

function triangle(offset, size){

console.log(offset);

var color = $("#color option:selected").val();

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

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

var width = size;

var height = size;

// Draw a path

context.beginPath();

//top of triangle

context.moveTo(offset + width/2, 0);

//top to right

context.lineTo(offset + width, size);

//bottom of triangle

context.lineTo(offset, size);

context.closePath();

// Fill the path

context.fillStyle = color;

context.fill();

}

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

var number = parseInt($("#number option:selected").val());

var shape = $("#shape option:selected").val();

var size = parseInt($("#size option:selected").val()) * 10;

var i = 0;

var position = 0;

var padding = size * 0.5; //leave space between the shapes 1/2 as large as the shape itself

while(i < number){

switch(shape){

case '1':

square(position, size);

break;

case '2':

circle(position, size);

break;

case '3':

triangle(position, size);

break;

}

i++;

// calculate the position of the next shape

position = position + size + padding;

}

});

})(jQuery);​

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值