From 751c462b555000946264c2f93c1e5db9d6f46dc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Mur=C3=A7a?= Date: Sat, 7 Jan 2023 14:17:31 -0300 Subject: [PATCH] Display colors used in each card --- src/file-renderer/index.js | 11 +++++++---- src/file-renderer/pattern.js | 8 +++++++- src/lib/CardList.svelte | 14 ++++++++++++-- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/file-renderer/index.js b/src/file-renderer/index.js index 594a918..9c94692 100644 --- a/src/file-renderer/index.js +++ b/src/file-renderer/index.js @@ -2,7 +2,7 @@ import { jDataView } from "./jdataview"; import { supportedFormats } from "../format-readers"; import { Pattern } from "./pattern"; -function renderToCanvas(filename, evt, canvas) { +function renderToCanvas(filename, evt, canvas, colorView) { const fileExtension = filename.toLowerCase().split(".").pop(); const view = jDataView(evt.target.result, 0, evt.size); const pattern = new Pattern(); @@ -10,7 +10,8 @@ function renderToCanvas(filename, evt, canvas) { supportedFormats[fileExtension].read(view, pattern); pattern.moveToPositive(); - pattern.drawShape(canvas); + pattern.drawShapeTo(canvas); + pattern.drawColorsTo(colorView); } function renderAbortMessage(errorMessageRef) { @@ -50,11 +51,13 @@ function renderErrorMessage(errorName, errorMessageRef) { export default function renderFileToCanvas( fileObject, canvas, - errorMessageRef + errorMessageRef, + colorView ) { const reader = new FileReader(); - reader.onloadend = (evt) => renderToCanvas(fileObject.name, evt, canvas); + reader.onloadend = (evt) => + renderToCanvas(fileObject.name, evt, canvas, colorView); reader.abort = (/** @type {any} */ _) => renderAbortMessage(errorMessageRef); reader.onerror = (evt) => renderErrorMessage(evt.target.error.name, errorMessageRef); diff --git a/src/file-renderer/pattern.js b/src/file-renderer/pattern.js index d81b7e4..c2c2f62 100644 --- a/src/file-renderer/pattern.js +++ b/src/file-renderer/pattern.js @@ -146,7 +146,7 @@ Pattern.prototype.fixColorCount = function () { this.colors.splice(maxColorIndex + 1, this.colors.length - maxColorIndex - 1); }; -Pattern.prototype.drawShape = function (canvas) { +Pattern.prototype.drawShapeTo = function (canvas) { canvas.width = this.right; canvas.height = this.bottom; if (canvas.getContext) { @@ -174,4 +174,10 @@ Pattern.prototype.drawShape = function (canvas) { } }; +Pattern.prototype.drawColorsTo = function (colorContainer) { + this.colors.forEach((color) => { + colorContainer.innerHTML += `
`; + }); +}; + export { Pattern, Color, stitchTypes }; diff --git a/src/lib/CardList.svelte b/src/lib/CardList.svelte index a2b7fc1..6ddd0a9 100644 --- a/src/lib/CardList.svelte +++ b/src/lib/CardList.svelte @@ -3,6 +3,7 @@ export let files = []; let canvasRefs = []; + let colorRefs = []; let errorMessageRef; @@ -12,9 +13,10 @@

{file.name}

+
{canvasRefs[i] && - renderFileToCanvas(file, canvasRefs[i], errorMessageRef)} + renderFileToCanvas(file, canvasRefs[i], errorMessageRef, colorRefs[i])} {/each}

@@ -36,6 +38,7 @@ justify-content: center; align-items: center; width: 550px; + /* Maybe use height: auto; */ height: 550px; margin-bottom: 15px; padding: 10px; @@ -48,10 +51,17 @@ object-fit: contain; } + .colors-container { + display: flex; + flex-wrap: wrap; + gap: 5px; + row-gap: 5px; + } + @media only screen and (max-device-width: 812px) { .canvas-container { width: 100%; - height: 400px; + height: auto; } #container {