这个JavaScript和canvas代码有什么问题



教程 : http://thecodeplayer.com/walkthrough/make-gauge-charts-using-canvas-and-javascript 定制 (工作) : http://jsfiddle.net/lewisgoddard/JyGkQ/29/
查看(不工作) : http://gamesforubuntu.org/review/review/show/hedgewars

就像我关闭脚本一样(在完成的页面中),Chrome 会抛出错误:

Uncaught SyntaxError: Unexpected token ILLEGAL 

我真的不知道出了什么问题。

</script>和最后一个右括号之间有一个不可见的字符。看看最后一行:

<script type="text/javascript" language="javascript">
window.onload = function(){
  // Canvas Initialization
    var canvas = document.getElementById("Performance"); // Get the canvas by ID
    var ctx = canvas.getContext("2d"); // Make it Flat
    // Dimensions
    var W = canvas.width; // Get the Width
    var H = canvas.height; // Get the Height
    //Variables
    var degrees = 0; // Start Position
    var percent = 93; // End Position
    var new_degrees = Math.round((percent+1)*360/100); // Figure out how far to go
    var difference = 0; // Set the Default Difference
//   Blue     19B6EE   BAE9FA
//   Green    38B44A   C3E8C9
//   Yellow   EFB73E   FAE9C5
//   Red      DF382C   F5C3C0
    if (percent<30) {
        var color = "#DF382C"; // Red
        var bgcolor = "#F5C3C0"; 
    } else if (percent<60) {
        var color = "#EFB73E"; // Yellow
        var bgcolor = "#FAE9C5";
    } else if (percent<90) {
        var color = "#38B44A"; // Green
        var bgcolor = "#C3E8C9";
    } else {
        var color = "#19B6EE"; // Blue
        var bgcolor = "#BAE9FA";
    }
    var text;
    var animation_loop, redraw_loop;
    function init()
    {
        //Clear the canvas everytime a chart is drawn
        ctx.clearRect(0, 0, W, H);
        //Background 360 degree arc
        ctx.beginPath();
        ctx.strokeStyle = bgcolor;
        ctx.lineWidth = 30;
        ctx.arc(W/2, H/2, 100, 0, Math.PI*2, false); //you can see the arc now
        ctx.stroke();
        //gauge will be a simple arc
        //Angle in radians = angle in degrees * PI / 180
        var radians = degrees * Math.PI / 180;
        ctx.beginPath();
        ctx.strokeStyle = color;
        ctx.lineWidth = 30;
        //The arc starts from the rightmost end. If we deduct 90 degrees from the angles
        //the arc will start from the topmost end
        ctx.arc(W/2, H/2, 100, 0 - 90*Math.PI/180, radians - 90*Math.PI/180, false); 
        //you can see the arc now
        ctx.stroke();
        //Lets add the text
        ctx.fillStyle = color;
        ctx.font = "50px";
        text = Math.floor(degrees/360*100) + "%";
        //Lets center the text
        //deducting half of text width from position x
        text_width = ctx.measureText(text).width;
        //adding manual value to position y since the height of the text cannot
        //be measured easily. There are hacks but we will keep it manual for now.
        ctx.fillText(text, W/2 - text_width/2, H/2 + 15);
    }
    function draw()
    {
        //Cancel any movement animation if a new chart is requested
        if(typeof animation_loop != undefined) clearInterval(animation_loop);
        //random degree from 0 to 360
        difference = new_degrees - degrees;
        //This will animate the gauge to new positions
        //The animation will take 1 second
        //time for each frame is 1sec / difference in degrees
        animation_loop = setInterval(animate_to, 1000/difference);
    }
    //function to make the chart move to new degrees
    function animate_to()
    {
        //clear animation loop if degrees reaches to new_degrees
        if(degrees == new_degrees) 
            clearInterval(animation_loop);
        if(degrees < new_degrees)
            degrees++;
        else
            degrees--;
        init();
    }
    draw();
}
<- Here was your enemy!</script>

最新更新