Skip to content

Instantly share code, notes, and snippets.

@ymrl
Last active August 29, 2015 14:22
Show Gist options
  • Save ymrl/a97fe0a26fbfd99f5653 to your computer and use it in GitHub Desktop.
Save ymrl/a97fe0a26fbfd99f5653 to your computer and use it in GitHub Desktop.
<html>
<head>
</head>
<body>
<canvas id="smb" width="500" height="500" style="border: 1px solid #ccc"></canvas>
<button type="button" id="black" style="background: #463F39; color: #fff;">#463F39</button>
<button type="button" id="skin" style="background: #F6BF86; color: #fff;">#F6BF86</button>
<button type="button" id="green" style="background: #C8F7B4; color: #fff;">#C8F7B4</button>
<button type="button" id="url">Get Image</button>
<button type="button" id="clear">Clear</button>
<script>
var BLACK = '#463F39', SKIN ='#F6BF86', GREEN = '#C8F7B4',
current = BLACK,
canvas = document.querySelector('#smb'),
context = canvas.getContext('2d');
mouseX = mouseY = -1;
context.lineWidth = 20;
context.lineCap = 'round';
canvas.addEventListener('mousedown', function(e){
mouseX = e.clientX;
mouseY = e.clientY;
});
canvas.addEventListener('mousemove', function(e){
if(mouseX < 0 || mouseY < 0){ return; }
context.beginPath();
context.strokeStyle = current;
context.moveTo(mouseX, mouseY);
mouseX = e.clientX;
mouseY = e.clientY;
context.lineTo(mouseX, mouseY);
context.stroke();
context.closePath()
});
canvas.addEventListener('mouseup', function(e){
mouseX = mouseY = -1;
});
document.querySelector('#black').addEventListener('click',function(){ current = BLACK; })
document.querySelector('#skin').addEventListener('click',function(){ current = SKIN; })
document.querySelector('#green').addEventListener('click',function(){ current = GREEN; })
document.querySelector('#url').addEventListener('click',function(){
window.open(canvas.toDataURL());
});
document.querySelector('#clear').addEventListener('click',function(){
if(window.confirm('are you sure?')){
context.clearRect(0,0,500,500);
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment