Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 27, 2023 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/3f9abb22b4e2e06993116456197bc482 to your computer and use it in GitHub Desktop.
Save code-boxx/3f9abb22b4e2e06993116456197bc482 to your computer and use it in GitHub Desktop.
Javascript Drawing PWA

JAVASCRIPT DRAWING PWA

https://code-boxx.com/javascript-drawing-app/

IMAGES

favicon icon-512

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html lang="en">
<head>
<!-- TITLE + CHARSET + DESCRIPTION + VIEWPORT + FAVICON -->
<title>JavaScript Drawing App</title>
<meta charset="utf-8">
<meta name="description" content="Offline Javascript Drawing App">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.5">
<link rel="icon" href="favicon.png" type="image/png">
<!-- ANDROID + CHROME + APPLE + WINDOWS APP -->
<meta name="mobile-web-app-capable" content="yes">
<meta name="theme-color" content="white">
<link rel="apple-touch-icon" href="icon-512.png">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="JS Draw">
<meta name="msapplication-TileImage" content="icon-512.png">
<meta name="msapplication-TileColor" content="#ffffff">
<!-- WEB APP MANIFEST -->
<!-- https://web.dev/add-manifest/ -->
<link rel="manifest" href="3a-manifest.json">
<!-- SERVICE WORKER -->
<script>
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("3b-worker.js");
}
</script>
<!-- STYLESHEET + JAVASCRIPT -->
<link rel="stylesheet" href="2-draw.css">
<script src="2-draw.js"></script>
</head>
<body>
<div id="wrapper">
<!-- (A) CANVAS -->
<canvas id="canvas"></canvas>
<!-- (B) DRAW CONTROLS -->
<div id="controls">
<input type="button" value="Clear" id="cClear">
<input type="color" id="cColor">
<input type="number" min="1" max="9" value="1" id="cSize">
<a id="cSave" download="draw.jpg">Save</a>
</div>
</div>
</body>
</html>
/* (A) WHOLE SITE */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
/* (B) WRAPPER */
#wrapper {
width: 600px;
margin: 0 auto;
}
/* (C) CANVAS */
#canvas {
width: 600px;
height: 400px;
border: 1px solid black;
}
/* (D) CONTROLS */
#controls {
display: flex;
border: 1px solid #aaa;
}
#controls * {
height: 50px;
padding: 10px;
border: 0;
}
#cClear, #cColor, #cSave { cursor: pointer; }
#cColor { flex-grow: 1; }
#cClear {
color: #fff;
background: #c51c1c;
}
#cSave {
display: flex;
color: #fff;
background: #4e74cb;
align-items: center;
justify-content: center;
}
// CREDITS
// https://dev.to/0shuvo0/lets-create-a-drawing-app-with-js-4ej3
// https://riptutorial.com/html5-canvas/example/11659/detecting-mouse-position-on-the-canvas
var draw = {
// (A) PROPERTIES
canvas : null, // html canvas
ctx : null, // html canvas context
px: null, py : null, // previous mouse coordinates
cx: null, cy : null, // current mouse coordinates
go : false, // flag to control paint
// (B) INIT
init : () => {
// (B1) GET HTML ELEMENTS
draw.canvas = document.getElementById("canvas");
draw.ctx = draw.canvas.getContext("2d");
// (B2) SET CANVAS WIDTH & HEIGHT
draw.canvas.width = draw.canvas.offsetWidth;
draw.canvas.height = draw.canvas.offsetHeight;
// (B3) DEFAULT DRAW SETTINGS
draw.ctx.fillStyle = "#ffffff";
draw.ctx.strokeStyle = "#000000";
draw.ctx.lineWidth = 1;
draw.ctx.fillRect(0, 0, draw.canvas.width, draw.canvas.height);
// (B4) ATTACH DRAW CONTROLS
// (B4-1) CLEAR CANVAS
document.getElementById("cClear").onclick = () => draw.ctx.fillRect(0, 0, draw.canvas.width, draw.canvas.height);
// (B4-2) DRAW COLOR
document.getElementById("cColor").onchange = e => draw.ctx.strokeStyle = e.target.value;
// (B4-3) DRAW SIZE
document.getElementById("cSize").onchange = e => draw.ctx.lineWidth = e.target.value;
// (B4-4) SAVE CANVAS
document.getElementById("cSave").onclick = e => e.target.href = draw.canvas.toDataURL("image/jpg");
// (B5) ENGAGE/DISENGAGE PAINT
let disengage = () => { draw.px = null; draw.py = null; draw.go = false; };
draw.canvas.addEventListener("mousedown", e => draw.go = true);
draw.canvas.addEventListener("mouseup", disengage);
draw.canvas.addEventListener("mouseout", disengage);
draw.canvas.addEventListener("mousemove", draw.paint);
},
// (C) PAINT
paint : e => { if (draw.go) {
// (C1) GET CANVAS MOUSE COORDINATES
let cRect = draw.canvas.getBoundingClientRect();
draw.cx = Math.round(e.clientX - cRect.left);
draw.cy = Math.round(e.clientY - cRect.top);
// (C2) SET INITIAL COORDINATES
if (draw.px == null || draw.py == null) {
draw.px = draw.cx;
draw.py = draw.cy;
}
// (C3) DRAW
draw.ctx.beginPath();
draw.ctx.moveTo(draw.px, draw.py);
draw.ctx.lineTo(draw.cx, draw.cy);
draw.ctx.stroke();
// (C4) UPDATE COORDINATES
draw.px = draw.cx;
draw.py = draw.cy;
}}
};
window.onload = draw.init;
{
"short_name": "Draw",
"name": "JS Draw",
"icons": [{
"src": "favicon.png",
"sizes": "64x64",
"type": "image/png"
}, {
"src": "icon-512.png",
"sizes": "512x512",
"type": "image/png"
}],
"start_url": "1-draw.html",
"scope": "/",
"background_color": "white",
"theme_color": "white",
"display": "standalone"
}
// (A) CREATE/INSTALL CACHE
self.addEventListener("install", evt => {
self.skipWaiting();
evt.waitUntil(
caches.open("JSDraw")
.then(cache => cache.addAll([
"1-draw.html",
"2-draw.css",
"2-draw.js",
"favicon.png",
"icon-512.png"
]))
.catch(err => console.error(err))
);
});
// (B) CLAIM CONTROL INSTANTLY
self.addEventListener("activate", evt => self.clients.claim());
// (C) LOAD FROM CACHE FIRST, FALLBACK TO NETWORK IF NOT FOUND
self.addEventListener("fetch", evt => evt.respondWith(
caches.match(evt.request).then(res => res || fetch(evt.request))
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment