var paper = Raphael("canvas",500,500);
var metronome = function(opts) {
// if no options specified, make an empty object
opts = opts || {};
//default values for variables if unspecified
var len = opts.len || 200, // length of metronome arm
angle = opts.angle || 20, //max angle from upright
width = len * Math.cos(angle),
x = opts.x || 0,
y = opts.y || 0,
tempo = 100;
// pieces of the metronome
var arm = paper.path("M" + (x + width) + "," + y + "v" + len).attr({
'stroke-width': 5,
stroke: "#999"
});
var weight = paper.path("M" + (x+width) + "," + (y+len) + "h9l3,-18h-24l3,18h12")
.attr({
'stroke-width': 0,
fill: '#666'
});
/* Next, let's set the weight at the center of the arm, make a label for the tempo, and use a drag event to allow the user to adjust the tempo by moving it. */
//start the weight at the halfway point (18px less than length bc of weight)
weight.position = (len - 18) / 2;
weight.transform("T0,-" + weight.position);
var label = paper.text(x + width, y + len + 15, tempo);
// track drag position
var drag_position;
weight.drag(
function(x, y, dx, dy, e) {
// restrict weight to top of bar and very bottom, allowing for 18px height of weight
drag_position = Math.min(len - 18, Math.max(0, this.position - y));
// calculate tempo based on position, with range of 20 bpm to 180 bpm
tempo = Math.round(20 + 160 * drag_position / (len - 18));
label.attr("text", tempo);
this.transform('T0,-' + drag_position);
},
function(x, y, e) {
this.attr("fill", "#99F");
},
function(e) {
this.position = drag_position;
this.attr("fill", "#666");
}
);
return {
play:...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.