Skip to content

Commit 5e40aa5

Browse files
committed
[ADD] basic option for background image drawing
closes RomiRand#1
1 parent 0f9c773 commit 5e40aa5

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

canvas.html

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<button class = "mmBtn" id="connectButton">Connect</button>
1313
<button class = "mmBtn" id="hideBtn"></button>
1414
<button class = "mmBtn" id="drawModeBtn">random</button>
15+
<button class = "mmBtn" id="backgroundBtn">Normal</button>
1516
</div>
1617
<div class="btns">
1718
<div class="afterImport">

index.js

+47-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const importButton = document.getElementById('importBtn');
55
const baseCanvas = document.getElementById('baseCanvas');
66
const drawCanvas = document.getElementById('drawCanvas');
77
const drawModeButton = document.getElementById('drawModeBtn');
8+
const backgroundButton = document.getElementById('backgroundBtn');
89

910
const queryString = window.location.search
1011
const urlParams = new URLSearchParams(queryString)
@@ -423,6 +424,7 @@ async function updateConnectButton()
423424
else if (img.image.src === "")
424425
{
425426
drawModeButton.style.visibility = "hidden"
427+
backgroundButton.style.visibility = "hidden"
426428
connectButton.innerHTML = "Drop or import image"
427429
connectButton.disabled = true
428430
}
@@ -433,16 +435,20 @@ async function updateConnectButton()
433435
befImport.children[i].disabled = false
434436
}
435437
drawModeButton.style.visibility = "visible"
438+
backgroundButton.style.visibility = "visible"
436439
connectButton.className = 'mmBtn'
437440
connectButton.innerHTML = 'draw!'
438441
connectButton.disabled = false
442+
backgroundButton.disabled = false
439443
}
440444
else
441445
{
442446
let befImport = document.getElementsByClassName("beforeImport")[0]
443447
for (let i = 0; i < befImport.children.length; i++) {
444448
befImport.children[i].disabled = true
445449
}
450+
// lock in decision
451+
backgroundButton.disabled = true
446452
connectButton.innerHTML = ''
447453
connectButton.className = 'mmBtnDrawing'
448454
}
@@ -558,7 +564,7 @@ async function getCurrentColor(x, y)
558564
{
559565
const imageData = baseCtx.getImageData(x, y, 1, 1).data
560566
if (imageData[3] === 0) // transparent background => white
561-
return "FFFFFF"
567+
return "not set"
562568

563569
return (imageData[0].toString(16).padStart(2, '0') +
564570
imageData[1].toString(16).padStart(2, '0') +
@@ -601,6 +607,7 @@ function approximateColor(r, g, b)
601607
}
602608

603609
let drawMode = "random"
610+
let background = false
604611
let drawing = false
605612
async function draw()
606613
{
@@ -613,35 +620,52 @@ async function draw()
613620
{
614621
if (!drawing) // shouldn't be set from outside...
615622
break
623+
let percent = 100 * (total - idx_array.length) / total
624+
progressBar[1].style.width = percent + '%'
625+
progressBar[0].innerHTML = percent.toFixed(2) + "% drawn (" + (total - idx_array.length) + "/" + total + " Pixel)"
616626
if (idx_array.length === 0)
617627
{
628+
if (background)
629+
break
618630
await delay(1000)
619631
continue
620632
}
621-
let percent = 100 * (total - idx_array.length) / total
622-
progressBar[1].style.width = percent + '%'
623-
progressBar[0].innerHTML = percent.toFixed(2) + "% drawn (" + (total - idx_array.length) + "/" + total + " Pixel)"
633+
624634
let idx
625635
if (drawMode === "random")
626636
idx = getRandomInt(idx_array.length)
627637
else if (drawMode === "rows")
628638
idx = 0
629639
let i = idx_array[idx] * 4
640+
641+
// get current color
642+
const x = img.x + (i / 4 % img.w)
643+
const y = img.y + Math.trunc(((i / 4) / img.w))
644+
let cur_col = await getCurrentColor(x, y)
645+
if (background && cur_col !== "not set")
646+
{
647+
idx_array.splice(idx, 1)
648+
continue
649+
}
650+
if (cur_col === "not set")
651+
{
652+
cur_col = "FFFFFF"
653+
}
654+
655+
// get new color
630656
const red = imgData[i];
631657
const green = imgData[i + 1];
632658
const blue = imgData[i + 2];
633659
const alpha = imgData[i + 3];
634-
if (alpha <= 16) // almost transparent, we can probably hide it
635-
{
660+
if (alpha <= 16)
661+
{// almost transparent, we can probably hide it
636662
idx_array.splice(idx, 1)
637663
continue
638664
}
639-
640665
let min_idx = approximateColor(red, green, blue)
641-
const x = img.x + (i / 4 % img.w)
642-
const y = img.y + Math.trunc(((i / 4) / img.w))
643-
const col = await getCurrentColor(x, y);
644-
if (col !== palette[min_idx])
666+
667+
// check if we need to update
668+
if (cur_col !== palette[min_idx])
645669
await paintPixel(x, y, min_idx)
646670
else
647671
idx_array.splice(idx,1)
@@ -771,7 +795,7 @@ drawCanvas.addEventListener("drop", function (evt) {
771795
var files = evt.dataTransfer.files;
772796
if (files.length > 0) {
773797
var file = files[0];
774-
if (typeof FileReader !== "undefined" && file.type.indexOf("image") != -1) {
798+
if (typeof FileReader !== "undefined" && file.type.indexOf("image") !== -1) {
775799
var reader = new FileReader();
776800
// Note: addEventListener doesn't work in Google Chrome for this event
777801
reader.onload = function (evt2) {
@@ -808,3 +832,14 @@ drawModeButton.addEventListener("click", async function(event)
808832
drawMode = "random"
809833
drawModeButton.innerText = drawMode
810834
})
835+
836+
backgroundButton.addEventListener("click", async function(event)
837+
{
838+
let backgroundStr
839+
background = !background
840+
if (background)
841+
backgroundStr = "Background only"
842+
else
843+
backgroundStr = "Normal"
844+
backgroundButton.innerText = backgroundStr
845+
})

styles.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ table#EnemyTable
106106
background-size: 75%;
107107
}
108108

109-
#drawModeBtn
109+
#drawModeBtn, #backgroundBtn
110110
{
111111
visibility: hidden;
112112
}

0 commit comments

Comments
 (0)