Skip to content

Commit 4554f5a

Browse files
committed
Add save btn for canvas in paint-win-95
1 parent e1ad3a3 commit 4554f5a

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

09-paint-win-95/icons/save.png

1010 Bytes
Loading

09-paint-win-95/index.html

+25-2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@
137137
background-image: url('./icons/trash.png');
138138
background-size: 18px;
139139
}
140+
141+
#save-btn {
142+
background-image: url('./icons/save.png');
143+
}
140144
</style>
141145
<script type="module">
142146
// CONSTANTS
@@ -145,7 +149,8 @@
145149
ERASE: 'erase',
146150
RECTANGLE: 'rectangle',
147151
ELLIPSE: 'ellipse',
148-
PICKER: 'picker'
152+
PICKER: 'picker',
153+
SAVE: 'save'
149154
}
150155

151156
// UTILITIES
@@ -160,6 +165,7 @@
160165
const $eraseBtn = $('#erase-btn')
161166
const $rectangleBtn = $('#rectangle-btn')
162167
const $pickerBtn = $('#picker-btn')
168+
const $saveBtn = $('#save-btn')
163169

164170
const ctx = $canvas.getContext('2d')
165171

@@ -200,6 +206,9 @@
200206
setMode(MODES.DRAW)
201207
})
202208

209+
$saveBtn.addEventListener('click', () => {
210+
setMode(MODES.SAVE)
211+
})
203212

204213
// METHODS
205214
function startDrawing(event) {
@@ -324,6 +333,19 @@
324333

325334
return
326335
}
336+
337+
if (mode === MODES.SAVE) {
338+
ctx.globalCompositeOperation='destination-over';
339+
ctx.fillStyle = 'white';
340+
ctx.fillRect(0, 0, canvas.width, canvas.height);
341+
342+
const link = document.createElement('a');
343+
link.href = canvas.toDataURL()
344+
link.download = 'my-paint.png';
345+
link.click()
346+
setMode(previousMode)
347+
return
348+
}
327349
}
328350

329351
function handleKeyDown({ key }) {
@@ -371,6 +393,7 @@ <h1>
371393
<button id="ellipse-btn" title="Elipse"></button>
372394
<button disabled id="picker-btn" title="Capturar Color"></button>
373395
<button id="clear-btn" title="Limpiar dibujo"></button>
396+
<button id="save-btn" title="Guardar dibujo"></button>
374397
</nav>
375398
</aside>
376399

@@ -384,4 +407,4 @@ <h1>
384407
</div>
385408
</body>
386409

387-
</html>
410+
</html>

0 commit comments

Comments
 (0)