Canvas.background
Set the background color of the canvas.
Parameters
- color
- Color of backgound
Sample
canvas.background("LightGrey");
Set the background color of the canvas.
canvas.background("LightGrey");
Set the size of the canvas.
canvas.size(100,600);
Create a circle.
var circle = canvas.circle(70, 70, 40, 0, 360);
Create a line.
var line1 = canvas.line(40, 20, 40, 120); var line2 = canvas.line(40, 120, 100, 20); var line3 = canvas.line(100, 20, 100, 120);
Create a rectangle.
var rect = canvas.rect(20, 20, 40, 40);
Draw a path according to W3C SVG Path specifiation.
Attention Chaining curve is not working properly.
var path = canvas.path("M17.078,22.004l-1.758-4.129l-2.007,4.752l-7.519-3.289l0.174,3.905l9.437,4.374l10.909-5.365l-0.149-4.989L17.078,22.004zM29.454,6.619L18.521,3.383l-3.006,2.671l-3.091-2.359L1.546,8.199l3.795,3.048l-3.433,5.302l10.879,4.757l2.53-5.998l2.257,5.308l11.393-5.942l-3.105-4.709L29.454,6.619zM15.277,14.579l-9.059-3.83l9.275-4.101l9.608,3.255L15.277,14.579z");
Draw text.
canvas.text(20, 60, "Hello"); canvas.text(30, 100, "World!");
Create a layer used to do actions on a group of shapes.
layer = canvas.layer(0, 0, 200, 200); layer.rect(40, 40, 60, 60).attribute(style); layer.rect(80, 80, 60, 60).attribute(style); layer.translate(10,10);
When new objects are created, they are not display immediatly. You need to refresh the canvas with a call to repaint.
canvas.text(0, 40, "Hello"); canvas.repaint();
Clean the canvas.
canvas.text(20, 40, "Clean Me!"); canvas.clear();
Run an animation. In the animation function, repaint is automatically called.
var circle = canvas9.circle(50, 70, 30, 0, 360).attribute(style);
var deltax = deltay = 1;
var anim = canvas9.anim(function() {
circle.attr.x += deltax;
circle.attr.y += deltay;
if(circle.attr.x>105) deltax = -deltax;
if(circle.attr.x<35) deltax = -deltax;
if(circle.attr.y>105) deltay = -deltay;
if(circle.attr.y<35) deltay = -deltay;
});
Remove an object from canvas
circle = canvas.circle(50, 50, 30, 0, 360); canvas.remove(circle); canvas.repaint();
Execute a function for each elements contained in the canvas.
canvas.forEach(function(obj) {
document.getElementById("output").innerHTML += 'A Circle!';
}, "circle");
Output:
Translate the object on the canvas from its position.
Attention This do not change the coordinates.
circle = canvas.circle(50, 50, 30, 0, 360); circle.translate(10,10); canvas.repaint();
Rotate the object on the canvas.
Attention This do not change the angle of the object.
rect = canvas.rect(40, 40, 60, 60).attribute(style); rect.rotate(45); canvas.repaint();
Scale the object on the canvas.
rect = canvas.rect(40, 40, 60, 60); rect.scale(1, 2); canvas.repaint();
Execute the function when click mouse is down.
circle = canvas.circle(50, 50, 30, 0, 360);
circle.onMouseDown(function(){
document.getElementById("output").innerHTML += "Down! ";
});
Output:
Execute the function when mouse is over the object
circle = canvas.circle(50, 50, 30, 0, 360);
circle.onMouseOver(function(){
document.getElementById("output").innerHTML += "Over! ";
});
Output:
Set attributes of the shape.
rect = canvas.rect(25, 25, 50, 50);
rect.attribute({globalAlpha:0.5})
canvas.repaint();
Create an animation from an Attribute of the object.
rect = canvas.rect(40, 40, 60, 60);
rect.transition({globalAlpha:0.01}, 0.01);
...
rect.transition({globalAlpha:1}, 0.01);