Random Objects
The canvas below contains several elements: a path element, filled blue, a text element filled in red, and a series of 50 circles created in a for loop with the same center but increasing radii.
<div id="example1" style="text-align:center"> </div>
<script type="text/javascript">
var paper1 = new Raphael( document.getElementById('example1'), 800, 200 )
var tetraminoPath = "M 5, 5 L 55, 5 L 55, 55 L 105, 55 L 105, 105 L 55, 105 L 55, 155 L 5, 155 Z"
var tetramino = paper1.path( tetraminoPath )
tetramino.attr({ 'stroke': 'black', 'stroke-width':3, 'fill':'blue', 'stroke-dasharray': "--."})
for (i = 0; i < 50; i++ ) {
paper1.circle( 250, 100, i*2 )
}
var t = paper1.text( 500, 50, "Welcome to Raphael" ).attr({"font-size": 50, 'fill':'red'})
</script>
Regular Polygons
The function in the example below generates a regular polygon given a location of its center, the number of sides needed, and the radius of the shape. The example shows the function in use to create several shapes on the canvas.
<div id="example2" style="text-align:center"> </div>
<script type="text/javascript">
var paper2 = new Raphael( document.getElementById('example2'), 800, 200 )
function regPoly(x, y, n, s) { // x,y = center location, n = number of sides, s = radius
var path = "", nn, nx, ny, angle;
for (nn = 0; nn <= n; nn += 1) {
angle = nn / n * 2 * Math.PI;
nx = x + Math.cos(angle) * s;
ny = y + Math.sin(angle) * s;
path += (nn === 0 ? "M" : "L") + nx + "," + ny;
}
return path;
}
// overlapping triangles:
paper2.path( regPoly(50, 100, 3, 60) ).attr({ 'fill': 'blue', 'stroke-width':0});
paper2.path( regPoly(50, 100, 3, 30) ).attr({ 'fill': 'red', 'stroke-width':0, 'opacity':0.6});
paper2.path( regPoly(150, 100, 5, 40) );
paper2.path( regPoly(250, 100, 8, 30) ).attr({ 'fill': 'green', 'stroke-width':2});
paper2.path( regPoly(350, 100, 12, 40) ).attr({ 'fill': '#bbb', 'stroke-width':2, 'stroke':'#aa0000'});
paper2.path( regPoly(450, 100, 16, 30) );
paper2.path( regPoly(550, 100, 40, 40) ).attr({ 'fill': 'yellow', 'stroke-width':1, 'stroke':'red'});
</script>
Sine and Cosine
In the example below, curves representing sine and cosine are drawn on a canvas called 'paper1', which is defined to be 800 pixels wide and 200 pixels high.
The difficulty with plotting functions is that the units in the canvas are pixels, which is usually not what we want to work with. For instance, the values of sine and cosine are always between -1 and 1, so if we use these values as our coordinates, the graph will only be two pixels high! Instead, we can "convert" from the canvas units to graph units, and back again. In the example below, I've defined a unit size of 50, meaning that 50 pixels on the screen will represent 1 unit on the number line. The sine and cosine functions take in an x-coordinate (in pixels), convert it using the predefined unit value, calculate sine or cosine using built in functions, then convert this value back into pixels.
Using these functions and a for loop, two curves are created in different ways. The sine curve is created by creating a series of small circles, packed so tightly together that they appear to form a continuous curve. The advantage of this method is that every circle can be a different color, so we're able to gradually change the color within the for loop and create a gradient effect. The cosine curve is created by constructing an SVG path, the building a path element. The advantage with this method is speed - a simple path is much more efficient than hundreds of tiny circles.
<div id="example3" style="text-align:center"> </div>
<script type="text/javascript">
var paper3 = new Raphael( document.getElementById('example3'), 800, 200 )
var w = 800 // variable to store width, in case we want to adjust later
var h = 200 // store height
var unit = 50 // unit size - 50 pixels will equal a distance of 1
function sine(gx) { // calculate sine of x (in pixels)
var x = (gx - w/2)/unit;
var y = Math.sin(x);
var gy = h/2 - unit*y;
return gy
}
function cosine(gx){ // calculate cosine of x (in pixels)
var x = (gx - w/2)/unit;
var y = Math.cos(x);
var gy = h/2 - unit*y;
return gy
}
var numberDots = 600 // number of dots to use in sine curve
var sinDots = paper3.set() // create a set to store the dots in the sine curve, in case we want to move or change it
var cosPath = "M 0,"+cosine(0) // begin the path for the cosine curve
for (i = 0; i < numberDots; i++ ) {
var sinecolor = 'rgb('+(255-i*0.3)+',200,'+(50+i*0.1)+')' // calculate new fill color
// create a new dot:
var c = paper3.circle( i*w/numberDots, sine( i*w/numberDots ), 3 ).attr({ 'fill': sinecolor, 'stroke-width':0})
sinDots.push(c) // add sine dot to set
cosPath += "L" + i*w/numberDots + "," + cosine( i*w/numberDots ) // add to path for cosine
}
var cosGraph = paper3.path( cosPath ).attr({ 'stroke': 'red', 'stroke-width':2}) // draw cosine path on canvas
</script>