About
Explore
Events & Classes
Support Us
Join
Sign In
purple_ghost
View Project Page
Run
Fullscreen
Paint Pro v0.0.1
HTML
CSS
JS
<!-- Watch YouTube Original: https://www.youtube.com/watch?v=amXSWXQssww --> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <div class="top-bar"> <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="100" value="1"> </div> <canvas id="draw"></canvas> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </body> </html>
body { margin: 0; } .top-bar { display: flex; flex-direction: row; background-color: #5c1e5d; border-bottom: 2px solid black; position: absolute; width: 100%; } .top-bar * { margin: 5px 10px; } #draw { display: block; cursor: pointer; }
var canvas, ctx, brush = { x: 0, y: 0, color: '#000000', size: 1, 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); }); $('#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);