public
Created

  • Download Gist
forb.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css" media="screen">
body {
background:;
}
canvas {
border: 1px solid #CFCFCF;
background: #FFFAEB;
display: block;
}
#tools {
margin: 0;
padding: 0;
}
#tools ul {
list-style: none;
float: left;
margin: 0;
padding: 2px;
}
#tools ul.active {
background: #CFCFCF;
}
</style>
</head>
<body>
<canvas id="draw" width="500" height="500"></canvas>
<ul id="tools"></ul>
</ul>
<script>
var shapes = [];
 
function findShape(name) {
for (shape in shapes) {
if (shapes[shape].name == name) {
return shapes[shape];
}
}
}
 
var shape = function(spec) {
var that = {};
 
that.draw = function(options) {
options.ctx.strokeStyle = options.color;
options.ctx.fillStyle = options.color;
options.ctx.beginPath();
 
spec.draw(options);
 
options.ctx.closePath();
options.ctx.stroke();
options.ctx.fill();
}
 
that.name = spec.name || "unknown";
 
return that;
}
 
shapes.push(shape({
name: "triangle",
draw: function(o) {
o.ctx.moveTo(o.x, o.y - o.size);
o.ctx.lineTo(o.x - o.size, o.y + o.size);
o.ctx.lineTo(o.x + o.size, o.y + o.size);
}
}));
 
shapes.push(shape({
name: "square",
draw: function(o) {
o.ctx.rect(o.x - o.size / 2, o.y - o.size / 2, o.size, o.size);
}
}));
 
shapes.push(shape({
name: "diamond",
draw: function(o) {
o.ctx.moveTo(o.x, o.y - o.size);
o.ctx.lineTo(o.x - o.size / 2, o.y);
o.ctx.lineTo(o.x, o.y + o.size);
o.ctx.lineTo(o.x + o.size / 2, o.y);
}
}));
 
shapes.push(shape({
name: "heart",
draw: function(o) {
var s = o.size;
var x = o.x;
var y = o.y;
o.ctx.moveTo(x, y - s / 1.8);
o.ctx.bezierCurveTo(x, y - s * 1.25, x - s, y - s * 1.25, x - s, y - s / 1.8);
o.ctx.bezierCurveTo(x - s, y - s / 5, x - s / 2, y, x, y + s);
o.ctx.bezierCurveTo(x + s / 2, y, x + s, y - s / 5, x + s, y - s / 1.8);
o.ctx.bezierCurveTo(x + s, y - s * 1.25, x, y - s * 1.25, x, y - s / 1.8);
}
}));
 
shapes.push(shape({
name: "circle",
draw: function(o) {
o.ctx.arc(o.x, o.y, o.size, 0, Math.PI * 2, true);
}
}));
 
var activeShape = shapes[0];
var mouseDown = false;
 
document.onmousedown = function(evt) { mouseDown = true; }
 
document.onmouseup = function(evt) { mouseDown = false; }
 
function drawIt(evt) {
var options = {
ctx: this.getContext("2d"),
x: evt.pageX - $(this).offset().left,
y: evt.pageY - $(this).offset().top,
size: randomNumber(30),
color: randomColor()
};
 
activeShape.draw(options);
}
 
document.getElementById("draw").onmousedown = function(evt) {
drawIt.call(this, evt);
}
 
document.getElementById("draw").onmousemove = function(evt) {
if (! mouseDown) {
return;
}
drawIt.call(this, evt);
};
 
function randomColor() {
var red = randomHex();
var green = randomHex();
var blue = randomHex();
 
return red + green + blue;
}
 
function randomHex() {
return randomNumber(256).toString(16);
}
 
function randomNumber(max) {
return Math.floor(Math.random() * max);
}
 
for (var i = 0; i < shapes.length; i++) {
var shape = shapes[i];
var ul = document.createElement("ul");
var canvas = document.createElement("canvas");
 
$(ul).attr("data-shape", shape.name);
canvas.height = 80;
canvas.width = 80;
ul.appendChild(canvas);
document.getElementById("tools").appendChild(ul);
 
shapes[i].draw({
ctx: canvas.getContext("2d"),
x: 40,
y: 40,
size: 30,
color: randomColor()
});
 
ul.onclick = function() {
activeShape = findShape($(this).attr("data-shape"));
$("#tools ul").removeClass("active");
$(this).addClass("active");
}
}
 
$("#tools ul:first-child").click();
</script>
</html>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.