diff options
Diffstat (limited to 'html/liogo.js')
-rw-r--r-- | html/liogo.js | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/html/liogo.js b/html/liogo.js new file mode 100644 index 0000000..8e268d9 --- /dev/null +++ b/html/liogo.js @@ -0,0 +1,111 @@ +// Mini Logo engine +// Based on Liogo compiler from Lionel Laské +// http://liogo.sourceforge.net +enyo.kind({ + name: "Liogo", + published: { canvas: undefined, image: undefined }, + + // Create component + create: function() { + this.inherited(arguments); + this.commands = []; + this.initheading = 90; + this.canvasChanged(); + }, + + // Canvas property changed + canvasChanged: function(old) { + this.initx = this.canvas.attributes.width / 2; + this.inity = this.canvas.attributes.height / 2; + this.width = this.canvas.attributes.width; + this.height = this.canvas.attributes.height; + }, + + // Draw canvas an its content + draw: function() { + // Clear all + this.ctx = this.canvas.node.getContext('2d'); + this.ctx.clearRect(0, 0, this.width, this.height); + this.ctx.canvas.width = this.ctx.canvas.width; + this.ctx.strokeStyle = "blue"; + this.ctx.lineWidth = 2; + + // Set init pos + this.x = this.initx; + this.y = this.inity; + this.heading = this.initheading; + this.ctx.moveTo(this.x, this.y); + + // Draw each command + var i = 0; + var len = this.commands.length; + for (i = 0 ; i < len ; i++) { + var action = this.commands[i]; + switch(action.command) + { + case 'f': + this._do_forward(action.arg); + break; + case 'r': + this._do_right(action.arg); + break; + case 'l': + this._do_left(action.arg); + break; + } + this.ctx.moveTo(this.x, this.y); + } + + // Draw turtle + this.ctx.translate(this.x, this.y); + this.ctx.rotate((90-this.heading) * (Math.PI / 180)); + this.ctx.translate(-11,-14); + this.ctx.drawImage(this.image, 0, 0); + }, + + // Clear all content + clear: function() { + this.commands = []; + this.x = this.initx; + this.y = this.inity; + this.heading = 90; + }, + + // Forward turtle to the value + forward: function(value) { + this.commands.push({command: 'f', arg: value}); + }, + + // Turn left turtle to the angle value + left: function(value) { + this.commands.push({command: 'l', arg: value}); + }, + + // Turn right turtle to the angle value + right: function(value) { + this.commands.push({command: 'r', arg: value}); + }, + + // Process forward command, forward turtle using value and current heading + _do_forward: function(value) { + var radians = this.heading * (Math.PI/180); + this.x = this.x + value * Math.cos(radians); + this.y = this.y - value * Math.sin(radians); + this.ctx.lineTo(this.x, this.y); + this.ctx.stroke(); + }, + + // Process left command, just change heading + _do_left: function(value) { + this.heading += value; + if (this.heading > 360) + this.heading -= 360; + }, + + // Process right command, just change heading + _do_right: function(value) { + this.heading -= value; + if (this.heading < 0) + this.heading += 360; + } +}); |