Skip to content

Instantly share code, notes, and snippets.

@kevinfjbecker
Created July 27, 2011 19:15
Show Gist options
  • Save kevinfjbecker/1110135 to your computer and use it in GitHub Desktop.
Save kevinfjbecker/1110135 to your computer and use it in GitHub Desktop.
A drawing toy using HTML5 canvas. Draw an abstract flower, and output the drawing to the page as a png image.
<!DOCTYPE html>
<html>
<!--
Created using JS Bin
Source can be edited via /ecekaw/8/edit
-->
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>Artsy</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
</head>
<body>
<canvas id="canvas" width="320" height="356"></canvas>
<br/>
<button id="outputButton">output</button>
<button id="clearButton">clear</button>
<p/>
<div id="output"></div>
<script>
$(function(){
var ctx = $('#canvas')[0].getContext("2d");
var WIDTH = $("#canvas").width();
var HEIGHT = $("#canvas").height();
var CENTER_Y = HEIGHT / 2;
var CENTER_X = WIDTH / 2;
var clear = (function clear () {
ctx.save();
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, WIDTH, HEIGHT);
ctx.strokeRect(0.5, 0.5, WIDTH - 0.5, HEIGHT - 0.5);
ctx.restore();
return clear;
})();
var draw = function(x, y) {
ctx.beginPath();
ctx.moveTo(CENTER_X, CENTER_Y);
ctx.lineTo(x, y);
ctx.stroke();
};
$('#canvas').mousemove(function(e) {
var offset = $(this).offset();
var coords = {};
coords.x = e.clientX - $(this).offset().left;
coords.y = e.clientY - $(this).offset().top;
draw(coords.x, coords.y);
});
$('#outputButton').click(function () {
var img = $('#canvas')[0].toDataURL("image/png");
$('#output').prepend($('<img src="'+img+'"/><p/>'));
});
$('#clearButton').click(function () {
clear();
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment