Created
July 31, 2016 00:13
-
-
Save encody/288024b72e6e210c87485162f0ced36e to your computer and use it in GitHub Desktop.
Simple Drawing Web App
This file contains hidden or 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
| /* | |
| Video Guide: https://youtu.be/amXSWXQssww | |
| */ | |
| body { | |
| margin: 0; | |
| } | |
| .top-bar { | |
| display: flex; | |
| flex-direction: row; | |
| background-color: #3af; | |
| border-bottom: 2px solid black; | |
| position: absolute; | |
| width: 100%; | |
| } | |
| .top-bar * { | |
| margin: 5px 10px; | |
| } | |
| #draw { | |
| display: block; | |
| } |
This file contains hidden or 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
| /* | |
| Video Guide: https://youtu.be/amXSWXQssww | |
| */ | |
| var canvas, ctx, | |
| brush = { | |
| x: 0, | |
| y: 0, | |
| color: '#000000', | |
| size: 10, | |
| down: false, | |
| }, | |
| strokes = [], | |
| currentStroke = null; | |
| function redraw () { | |
| ctx.clearRect(0, 0, canvas.width(), canvas.height()); | |
| ctx.lineCap = 'round'; | |
| for (var i = 0; i < strokes.length; i++) { | |
| var s = strokes[i]; | |
| ctx.strokeStyle = s.color; | |
| ctx.lineWidth = s.size; | |
| ctx.beginPath(); | |
| ctx.moveTo(s.points[0].x, s.points[0].y); | |
| for (var j = 0; j < s.points.length; j++) { | |
| var p = s.points[j]; | |
| ctx.lineTo(p.x, p.y); | |
| } | |
| ctx.stroke(); | |
| } | |
| } | |
| function init () { | |
| canvas = $('#draw'); | |
| canvas.attr({ | |
| width: window.innerWidth, | |
| height: window.innerHeight, | |
| }); | |
| ctx = canvas[0].getContext('2d'); | |
| function mouseEvent (e) { | |
| brush.x = e.pageX; | |
| brush.y = e.pageY; | |
| currentStroke.points.push({ | |
| x: brush.x, | |
| y: brush.y, | |
| }); | |
| redraw(); | |
| } | |
| canvas.mousedown(function (e) { | |
| brush.down = true; | |
| currentStroke = { | |
| color: brush.color, | |
| size: brush.size, | |
| points: [], | |
| }; | |
| strokes.push(currentStroke); | |
| mouseEvent(e); | |
| }).mouseup(function (e) { | |
| brush.down = false; | |
| mouseEvent(e); | |
| currentStroke = null; | |
| }).mousemove(function (e) { | |
| if (brush.down) | |
| mouseEvent(e); | |
| }); | |
| $('#save-btn').click(function () { | |
| window.open(canvas[0].toDataURL()); | |
| }); | |
| $('#undo-btn').click(function () { | |
| strokes.pop(); | |
| redraw(); | |
| }); | |
| $('#clear-btn').click(function () { | |
| strokes = []; | |
| redraw(); | |
| }); | |
| $('#color-picker').on('input', function () { | |
| brush.color = this.value; | |
| }); | |
| $('#brush-size').on('input', function () { | |
| brush.size = this.value; | |
| }); | |
| } | |
| $(init); |
This file contains hidden or 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
| <!-- | |
| Video Guide: https://youtu.be/amXSWXQssww | |
| --> | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>Draw</title> | |
| <link rel="stylesheet" href="draw.css"> | |
| </head> | |
| <body> | |
| <div class="top-bar"> | |
| <button id="save-btn">Save</button> | |
| <button id="undo-btn">Undo</button> | |
| <button id="clear-btn">Clear</button> | |
| <input type="color" id="color-picker"> | |
| <input type="range" id="brush-size" min="1" max="50" value="10"> | |
| </div> | |
| <canvas id="draw"></canvas> | |
| <script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> | |
| <script src="draw.js"></script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment