Created
January 20, 2014 15:15
-
-
Save ohager/8521687 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function main(){ | |
var context = document.getElementById('myCanvas').getContext('2d'); | |
var painter = new Painter(context); | |
painter.setFillColor(255,0,0,1); | |
painter.drawText("Text 1", 50); | |
painter.pushState(); | |
painter.rotate(320, 100, 45); | |
painter.setFillColor(0,0,255,1); | |
painter.drawText("Text 2", 100); | |
painter.popState(); | |
painter.drawText("Text 3", 150); | |
} | |
function Painter(ctx){ | |
var context = ctx; | |
var DEG2RAD = Math.PI/180.0; | |
var center = {}; | |
var init = function(ctx){ | |
context = ctx; | |
center[0] = context.canvas.width/2; | |
center[1] = context.canvas.height/2; | |
}; | |
this.pushState = function(){ | |
context.save(); | |
}; | |
this.popState = function(){ | |
context.restore(); | |
}; | |
this.rotate = function(posX, posY, angle){ | |
context.translate(posX, posY); | |
context.rotate(angle * DEG2RAD); | |
context.translate(-posX, -posY); | |
}; | |
this.setFillColor = function(r,g,b,a){ | |
context.fillStyle = "rgba(" + r + "," + g + "," + b + "," + a +")"; | |
}; | |
this.drawText = function(text, ypos){ | |
context.font = "30px Arial"; | |
context.textAlign = "center"; | |
context.fillText(text, center[0], ypos); | |
}; | |
init(ctx); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment