Skip to content

Instantly share code, notes, and snippets.

@miyataken999
Created August 24, 2018 19:45
Show Gist options
  • Save miyataken999/d1c47a38e4e9f3c9cb8874dd1babde83 to your computer and use it in GitHub Desktop.
Save miyataken999/d1c47a38e4e9f3c9cb8874dd1babde83 to your computer and use it in GitHub Desktop.
Canvas APIを使ってお絵描き
<div class="options">
<p>色</p>
<input type="text" id="color">
<p>太さ</p>
<input type="text" id="weight">
</div>
<p><input type="button" value="Canvasクリア" id="clear"></p>
<canvas id="canvas" width="800" height="400"></canvas>
$(function(){
var canvas = $('#canvas');
var context = canvas[0].getContext('2d');
canvas.on('mousemove', function(e){
if(e.buttons === 1){
var
x = e.pageX - canvas.offset().left,
y = e.pageY - canvas.offset().top,
color = $('#color').val() || '#000',
weight = $('#weight').val() || 10;
context.fillStyle = color;
context.fillRect(x, y, weight, weight);
}
});
$('#clear').on('click', function(){
context.clearRect(0,0,800,400);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
.options{
margin-bottom: 20px;
}
#canvas{
border: 2px solid #ccc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment