Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debugging to examples/drawing_code.js #43

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions examples/drawing_code.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _canvas, _ctx;
var _canvas, _ctx, DEBUG = true;

window.onload = function() {
setTimeout(function() {
Expand All @@ -10,16 +10,19 @@ window.onload = function() {
};

function color(col) {
debug('setting color to', col);
_ctx.strokeStyle = _ctx.fillStyle = col;
}

function lineWidth(n) {
debug('setting line width to', n);
_ctx.lineWidth = n;
}

var _pushDepth = 0, _centerX, _centerY;

function clear() {
debug('clearing screen');
for (var i = 0; i < _pushDepth; ++i)
_ctx.restore();
var w = document.body.clientWidth - 5, h = document.body.clientHeight - 5;
Expand All @@ -32,23 +35,27 @@ function clear() {
}

function box(x, y, w, h) {
debug('drawing a', w, 'by', h, 'box at', x, y);
_ctx.fillRect(x, y - h, w, h);
}

function circle(x, y, r) {
debug('drawing a circle with radius', r, 'at', x, y);
_ctx.beginPath();
_ctx.arc(x, y, r, 0, 2 * Math.PI);
_ctx.fill();
}

function line(x1, y1, x2, y2) {
debug('drawing a line from', x1, y1, 'to', x2, y2);
_ctx.beginPath();
_ctx.moveTo(x1, y1);
_ctx.lineTo(x2, y2);
_ctx.stroke();
}

function path(spec) {
debug('starting a new curve');
_ctx.beginPath();
var parsed = spec.split(/\s+/g);
function arg() {
Expand All @@ -60,14 +67,19 @@ function path(spec) {
try {
for (var i = 0; i < parsed.length; ++i) {
var cmd = parsed[i];
var x = arg(), y = arg();
if (cmd == "c") {
debug('drawing a line back to the start of the curve');
_ctx.closePath();
} else if (cmd == "g") {
_ctx.moveTo(arg(), arg());
debug('moving to', x, y);
_ctx.moveTo(x, y);
} else if (cmd == "l") {
_ctx.lineTo(arg(), arg());
debug('drawing a straight line to', x, y);
_ctx.lineTo(x, y);
} else if (cmd == "q") {
var x = arg(), y = arg();
debug('drawing a curve to', x, y);
_ctx.quadraticCurveTo(arg(), arg(), x, y);
} else {
throw new Error("Unrecognized path command: '" + cmd + "'");
Expand All @@ -80,6 +92,7 @@ function path(spec) {
}

function text(x, y, string) {
debug('writing text', JSON.stringify(string), 'at', x, y);
_ctx.save();
_ctx.scale(1, -1);
_ctx.font = "16px sans-serif";
Expand All @@ -88,28 +101,39 @@ function text(x, y, string) {
}

function rotate(angle) {
debug('rotating coordinates by', angle, 'degrees');
_ctx.save();
++_pushDepth;
_ctx.rotate(angle * Math.PI / 180);
}

function moveTo(x, y) {
debug('moving origin of coordinates to', x, y);
_ctx.save();
++_pushDepth;
_ctx.translate(x, y);
}

function scale(factor) {
debug('scaling coordinates by a factor of', factor);
_ctx.save();
++_pushDepth;
_ctx.scale(factor, factor);
}

function goBack() {
debug('undoing the last change to the coordinates');
_ctx.restore();
--_pushDepth;
}

function fill(color) {
debug('Filling in with color', color);
_ctx.fill();
}

function debug() {
if (DEBUG) {
console.log.apply(console, arguments);
}
}