简体   繁体   English

HTML5 Canvas挑战

[英]HTML5 Canvas Challenge

I am creating an HTML5 app that will display a bunch of shapes in different colors. 我正在创建一个HTML5应用程序,它将以不同的颜色显示一堆形状。 I am having trouble display more than one of any shape. 我无法显示任何形状的多个图形。

Here is a JSFiddle link to my project: http://jsfiddle.net/tithos/3uyLc/ 这是我项目的JSFiddle链接: http : //jsfiddle.net/tithos/3uyLc/

Here is one of the things I tried: 这是我尝试过的事情之一:

$("#go").click(function() {
  var number = $("#number option:selected").val();
  var shape = $("#shape option:selected").val();
  var size = $("#size option:selected").val();
  var offset = size;
  var i = 0;
  var shift = 0;

  while(i < number){
    switch(shape){
      case '1':
        console.log(shift);
        square((offset+shift), size);
        shift = (shift + size);
        break;
      case '2':
        circle(offset, size);
        break;
      case '3':
        triangle(offset, size);
        break;
    }
    i++;
  }
});

This, when repeated 16 times, gives me "0121212121212121212121212121212" in the concole. 重复16次后,这句话就给了我“ 0121212121212121121121121212121212”。 It is concatenating, not adding. 它是串联的,而不是添加的。 Why? 为什么?

Any help or insights are welcome 欢迎任何帮助或见解

Thanks, 谢谢,

Tim 提姆

Since .val() returns a string you are using + operator between two strings, which is the concatenation operator. 由于.val()返回一个字符串,因此您在两个字符串之间使用+运算符,这是串联运算符。 Use parseInt to convert a string to integer. 使用parseInt将字符串转换为整数。

In the first few lines, you need to parseInt from each of the .val() functions. 在前几行中,您需要从每个.val()函数中解析parseInt。 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. 它们需要在主循环中完成,而drawShape方法每个都有在指定大小的给定位置绘制给定形状的任务。 http://jsfiddle.net/3uyLc/39/ 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);​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM