Skip to content

Instantly share code, notes, and snippets.

@ohager
Created January 20, 2014 15:15
Show Gist options
  • Save ohager/8521687 to your computer and use it in GitHub Desktop.
Save ohager/8521687 to your computer and use it in GitHub Desktop.
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