Programming 6: Canvas Shim

Put your programming skills to the test in these challenges.

Programming 6: Canvas Shim

Post by ctheiss on Thu Apr 09, 2015 4:54 pm
([msg=87652]see Programming 6: Canvas Shim[/msg])

Perhaps it's time to switch to canvas for this problem? Here's a shim I used to debug my solution.

Code: Select all
var old = $('#drawHere');
if (old) {
    old.remove();
}

var canvas = document.getElementsByTagName('canvas');
if (canvas.length) {
    canvas = canvas[0];
    ctx = canvas.getContext('2d');
    ctx.clearRect (0, 0, canvas.width, canvas.height);
} else {
    var canvas = document.createElement('canvas');
    canvas.setAttribute('width', 1000);
    canvas.setAttribute('height', 1000);
    document.body.appendChild(canvas);
    ctx = canvas.getContext('2d');
}

var array = '[' + drawIt.toString().match(/drawData = new Array\(([^\)]+)\)/)[1] + ']';
var drawData = JSON.parse(array);
var i = 0;
var radConv = Math.PI / -180;

while (i < drawData.length) {
    if (drawData[i+2]>=10) {
        var x1 = drawData[i++];
        var y1 = drawData[i++];
        var x2 = drawData[i++];
        var y2 = drawData[i++];

        ctx.beginPath();
        ctx.moveTo(x1, y1);
        ctx.lineTo(x2, y2);
        ctx.stroke();

    } else {
        var x = drawData[i++];
        var y = drawData[i++];
        var radius = drawData[i++];
        var start = drawData[i++];
        var len = drawData[i++];
        var end = start + len;

        ctx.beginPath();
        ctx.arc(x, y, radius, start * radConv, end * radConv, true);
        ctx.stroke();
    }
}
ctheiss
New User
New User
 
Posts: 1
Joined: Thu Apr 09, 2015 4:51 pm
Blog: View Blog (0)


Re: Programming 6: Canvas Shim

Post by cyberdrain on Thu Apr 09, 2015 6:01 pm
([msg=87655]see Re: Programming 6: Canvas Shim[/msg])

I'd hate to do this, because of the nice code, but isn't this technically a spoiler?
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests