Skip to content

Commit

Permalink
Add drag-and-drop sprite creation
Browse files Browse the repository at this point in the history
  • Loading branch information
YodaLightsabr committed Jan 29, 2022
1 parent a47a174 commit f3d4990
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
4 changes: 4 additions & 0 deletions dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ const ACTIONS = {
INIT(args, state) {
init(state);
},
SET_DROPPED_IMAGE({ file }, state) {
dispatch("CREATE_SPRITE");
state.pixelEditor.setImageData(file);
},
RUN(args, state) {
const string = state.codemirror.view.state.doc.toString();

Expand Down
7 changes: 6 additions & 1 deletion events.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ function readFile(file) {
reader.onloadend = (event) => {
let raw = reader.result;

const saved = JSON.parse(raw);
try {
const saved = JSON.parse(raw);
} catch (err) {

dispatch("SET_DROPPED_IMAGE", { file });
}

dispatch("UPLOAD", { saved });
};
Expand Down
37 changes: 36 additions & 1 deletion pixel-editor/pixel-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,32 @@ export function createPixelEditor(target) {
// hoveredCell: null,
};

const setImageData = (file) => {
let reader = new FileReader();
reader.onload = (event) => {
const dataURL = event.target.result;
const canvas = document.getElementById('offscreen-canvas');
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
const image = new Image();
image.onload = () => {
ctx.drawImage(image, 0, 0, 32, 32);
const imageData = ctx.getImageData(0, 0, 32, 32);
for (let i = 0; i < state.gridColors.length; i++) {
state.gridColors[i] = [
imageData.data[i * 4],
imageData.data[i * 4 + 1],
imageData.data[i * 4 + 2],
imageData.data[i * 4 + 3]
];
}
};
image.src = dataURL;
};
reader.readAsDataURL(file);

}

const renderTool = (toolName, state) => html`
<button
class=${[state.tool === toolName ? "selected-tool" : ""].join(" ")}
Expand All @@ -60,7 +86,15 @@ export function createPixelEditor(target) {
const view = (state) => html`
${pixelStyles}
<div class="canvas-container">
<canvas class="drawing-canvas"></canvas>
<canvas
class="offscreen-canvas"
id="offscreen-canvas"
width="32"
height="32"
></canvas>
<canvas
class="drawing-canvas"
></canvas>
</div>
<div class="toolbox">
${["draw", "circle", "rectangle", "line", "bucket", "move"].map((name) =>
Expand Down Expand Up @@ -541,5 +575,6 @@ export function createPixelEditor(target) {
).fill([0, 0, 0, 0]),
}),
gridColors: () => state.gridColors,
setImageData: setImageData
};
}
4 changes: 4 additions & 0 deletions pixel-editor/pixel-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export const pixelStyles = html`
background: white;
}
.offscreen-canvas {
display: none;
}
.toolbox {
position: absolute;
right: 10px;
Expand Down

0 comments on commit f3d4990

Please sign in to comment.