A Pen by tetsuro ebata on CodePen.
Created
August 24, 2018 19:45
-
-
Save miyataken999/d1c47a38e4e9f3c9cb8874dd1babde83 to your computer and use it in GitHub Desktop.
Canvas APIを使ってお絵描き
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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