mirror of
https://github.com/piskelapp/piskel.git
synced 2023-08-10 21:12:52 +03:00
Previews are fixed. Removed drawing methods from piskel .js
This commit is contained in:
parent
c261c370ad
commit
69a03a3416
@ -18,9 +18,7 @@
|
|||||||
|
|
||||||
<!-- Frame actions: -->
|
<!-- Frame actions: -->
|
||||||
<button onclick="piskel.storeSheet()" class="action-button">Save Framesheet</button>
|
<button onclick="piskel.storeSheet()" class="action-button">Save Framesheet</button>
|
||||||
<button id='add-frame-button' class="action-button">
|
<button id='add-frame-button' class="action-button">Add a Frame</button>
|
||||||
Add a Frame
|
|
||||||
</button>
|
|
||||||
|
|
||||||
<!-- List of frames: -->
|
<!-- List of frames: -->
|
||||||
<ul id="preview-list"></ul>
|
<ul id="preview-list"></ul>
|
||||||
|
@ -55,8 +55,12 @@ pskl.FrameSheetModel = (function() {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
addEmptyFrame: function() {
|
addEmptyFrame : function () {
|
||||||
frames.push(pskl.rendering.Frame.createEmpty(width, height));
|
this.addFrame(pskl.rendering.Frame.createEmpty(width, height));
|
||||||
|
},
|
||||||
|
|
||||||
|
addFrame: function(frame) {
|
||||||
|
frames.push(frame);
|
||||||
},
|
},
|
||||||
|
|
||||||
getFrameCount: function() {
|
getFrameCount: function() {
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
/**
|
/**
|
||||||
* @override
|
* @override
|
||||||
*/
|
*/
|
||||||
ns.Eraser.prototype.applyToolAt = function(col, row, frame) {
|
ns.Eraser.prototype.applyToolAt = function(col, row, color, drawer) {
|
||||||
this.superclass.applyToolAt.call(this, col, row, frame, Constants.TRANSPARENT_COLOR);
|
this.superclass.applyToolAt.call(this, col, row, Constants.TRANSPARENT_COLOR, drawer);
|
||||||
};
|
};
|
||||||
})();
|
})();
|
@ -73,7 +73,7 @@
|
|||||||
var nextCol = currentItem.col + dx[i]
|
var nextCol = currentItem.col + dx[i]
|
||||||
var nextRow = currentItem.row + dy[i]
|
var nextRow = currentItem.row + dy[i]
|
||||||
try {
|
try {
|
||||||
if (frame.isInFrame(nextCol, nextRow) && frame.getPixel(nextCol, nextRow) == targetColor) {
|
if (frame.containsPixel(nextCol, nextRow) && frame.getPixel(nextCol, nextRow) == targetColor) {
|
||||||
queue.push({"col": nextCol, "row": nextRow });
|
queue.push({"col": nextCol, "row": nextRow });
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
|
@ -52,7 +52,7 @@
|
|||||||
*/
|
*/
|
||||||
ns.Rectangle.prototype.releaseToolAt = function(col, row, color, drawer) {
|
ns.Rectangle.prototype.releaseToolAt = function(col, row, color, drawer) {
|
||||||
// If the stroke tool is released outside of the canvas, we cancel the stroke:
|
// If the stroke tool is released outside of the canvas, we cancel the stroke:
|
||||||
if(drawer.frame.isInFrame(col, row)) {
|
if(drawer.frame.containsPixel(col, row)) {
|
||||||
var strokePoints = this.getRectanglePixels_(this.startCol, col, this.startRow, row);
|
var strokePoints = this.getRectanglePixels_(this.startCol, col, this.startRow, row);
|
||||||
for(var i = 0; i< strokePoints.length; i++) {
|
for(var i = 0; i< strokePoints.length; i++) {
|
||||||
// Change model:
|
// Change model:
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
* @override
|
* @override
|
||||||
*/
|
*/
|
||||||
ns.SimplePen.prototype.applyToolAt = function(col, row, color, drawer) {
|
ns.SimplePen.prototype.applyToolAt = function(col, row, color, drawer) {
|
||||||
|
if (drawer.frame.containsPixel(col, row)) {
|
||||||
this.previousCol = col;
|
this.previousCol = col;
|
||||||
this.previousRow = row;
|
this.previousRow = row;
|
||||||
drawer.frame.setPixel(col, row, color);
|
drawer.frame.setPixel(col, row, color);
|
||||||
@ -26,6 +27,7 @@
|
|||||||
// Draw on canvas:
|
// Draw on canvas:
|
||||||
// TODO: Remove that when we have the centralized redraw loop
|
// TODO: Remove that when we have the centralized redraw loop
|
||||||
drawer.renderFramePixel(col, row);
|
drawer.renderFramePixel(col, row);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ns.SimplePen.prototype.moveToolAt = function(col, row, color, drawer) {
|
ns.SimplePen.prototype.moveToolAt = function(col, row, color, drawer) {
|
||||||
|
@ -69,7 +69,7 @@
|
|||||||
ns.Stroke.prototype.releaseToolAt = function(col, row, color, drawer) {
|
ns.Stroke.prototype.releaseToolAt = function(col, row, color, drawer) {
|
||||||
// If the stroke tool is released outside of the canvas, we cancel the stroke:
|
// If the stroke tool is released outside of the canvas, we cancel the stroke:
|
||||||
// TODO: Mutualize this check in common method
|
// TODO: Mutualize this check in common method
|
||||||
if(drawer.frame.isInFrame(col, row)) {
|
if(drawer.frame.containsPixel(col, row)) {
|
||||||
// The user released the tool to draw a line. We will compute the pixel coordinate, impact
|
// The user released the tool to draw a line. We will compute the pixel coordinate, impact
|
||||||
// the model and draw them in the drawing canvas (not the fake overlay anymore)
|
// the model and draw them in the drawing canvas (not the fake overlay anymore)
|
||||||
var strokePoints = this.getLinePixels_(this.startCol, col, this.startRow, row);
|
var strokePoints = this.getLinePixels_(this.startCol, col, this.startRow, row);
|
||||||
|
74
js/piskel.js
74
js/piskel.js
@ -9,7 +9,7 @@ $.namespace("pskl");
|
|||||||
/**
|
/**
|
||||||
* FrameSheetModel instance.
|
* FrameSheetModel instance.
|
||||||
*/
|
*/
|
||||||
var frameSheet,
|
var frameSheet, renderer = null,
|
||||||
|
|
||||||
// Temporary zoom implementation to easily get bigger canvases to
|
// Temporary zoom implementation to easily get bigger canvases to
|
||||||
// see how good perform critical algorithms on big canvas.
|
// see how good perform critical algorithms on big canvas.
|
||||||
@ -50,8 +50,18 @@ $.namespace("pskl");
|
|||||||
var piskel = {
|
var piskel = {
|
||||||
|
|
||||||
init : function () {
|
init : function () {
|
||||||
|
var emptyFrame = pskl.rendering.Frame.createEmpty(framePixelWidth, framePixelHeight);
|
||||||
|
|
||||||
|
this.drawingController = new pskl.rendering.DrawingController(
|
||||||
|
emptyFrame,
|
||||||
|
$('#drawing-canvas-container')[0],
|
||||||
|
drawingCanvasDpi
|
||||||
|
);
|
||||||
|
|
||||||
|
renderer = new pskl.rendering.FrameRenderer();
|
||||||
|
|
||||||
frameSheet = pskl.FrameSheetModel.getInstance(framePixelWidth, framePixelHeight);
|
frameSheet = pskl.FrameSheetModel.getInstance(framePixelWidth, framePixelHeight);
|
||||||
frameSheet.addEmptyFrame();
|
frameSheet.addFrame(emptyFrame);
|
||||||
this.setActiveFrame(0);
|
this.setActiveFrame(0);
|
||||||
|
|
||||||
pskl.NotificationService.init();
|
pskl.NotificationService.init();
|
||||||
@ -90,13 +100,6 @@ $.namespace("pskl");
|
|||||||
this.initAnimationPreview();
|
this.initAnimationPreview();
|
||||||
this.startAnimation();
|
this.startAnimation();
|
||||||
|
|
||||||
var frame = frameSheet.getFrameByIndex(this.getActiveFrameIndex());
|
|
||||||
this.drawer = new pskl.rendering.DrawingController(
|
|
||||||
frame,
|
|
||||||
$('#drawing-canvas-container')[0],
|
|
||||||
drawingCanvasDpi
|
|
||||||
);
|
|
||||||
|
|
||||||
pskl.ToolSelector.init();
|
pskl.ToolSelector.init();
|
||||||
pskl.Palette.init(frameSheet);
|
pskl.Palette.init(frameSheet);
|
||||||
},
|
},
|
||||||
@ -134,7 +137,7 @@ $.namespace("pskl");
|
|||||||
|
|
||||||
setActiveFrame: function(index) {
|
setActiveFrame: function(index) {
|
||||||
activeFrameIndex = index;
|
activeFrameIndex = index;
|
||||||
currentFrame = frameSheet.getFrameByIndex(activeFrameIndex)
|
this.drawingController.frame = frameSheet.getFrameByIndex(index);
|
||||||
},
|
},
|
||||||
|
|
||||||
setActiveFrameAndRedraw: function(index) {
|
setActiveFrameAndRedraw: function(index) {
|
||||||
@ -144,7 +147,7 @@ $.namespace("pskl");
|
|||||||
// trigger an event instead:
|
// trigger an event instead:
|
||||||
|
|
||||||
// Update drawing canvas:
|
// Update drawing canvas:
|
||||||
this.drawFrameToCanvas(currentFrame, drawingAreaCanvas, drawingCanvasDpi);
|
this.drawingController.renderFrame();
|
||||||
// Update slideshow:
|
// Update slideshow:
|
||||||
this.createPreviews();
|
this.createPreviews();
|
||||||
// Update animation preview:
|
// Update animation preview:
|
||||||
@ -160,13 +163,12 @@ $.namespace("pskl");
|
|||||||
|
|
||||||
initDrawingArea : function() {
|
initDrawingArea : function() {
|
||||||
drawingAreaContainer = $('#drawing-canvas-container')[0];
|
drawingAreaContainer = $('#drawing-canvas-container')[0];
|
||||||
var body = document.getElementsByTagName('body')[0];
|
document.body.addEventListener('mouseup', this.onMouseup.bind(this));
|
||||||
body.setAttribute('onmouseup', 'piskel.onDocumentBodyMouseup(event)');
|
drawingAreaContainer.addEventListener('mousedown', this.onMousedown.bind(this));
|
||||||
|
drawingAreaContainer.addEventListener('mousemove', this.onMousemove.bind(this));
|
||||||
drawingAreaContainer.style.width = framePixelWidth * drawingCanvasDpi + "px";
|
drawingAreaContainer.style.width = framePixelWidth * drawingCanvasDpi + "px";
|
||||||
drawingAreaContainer.style.height = framePixelHeight * drawingCanvasDpi + "px";
|
drawingAreaContainer.style.height = framePixelHeight * drawingCanvasDpi + "px";
|
||||||
drawingAreaContainer.setAttribute('oncontextmenu', 'piskel.onCanvasContextMenu(event)');
|
drawingAreaContainer.addEventListener('contextmenu', this.onCanvasContextMenu);
|
||||||
drawingAreaContainer.setAttribute('onmousedown', 'piskel.onCanvasMousedown(event)');
|
|
||||||
drawingAreaContainer.setAttribute('onmousemove', 'piskel.onCanvasMousemove(event)');
|
|
||||||
},
|
},
|
||||||
|
|
||||||
initPreviewSlideshow: function() {
|
initPreviewSlideshow: function() {
|
||||||
@ -208,6 +210,7 @@ $.namespace("pskl");
|
|||||||
},
|
},
|
||||||
|
|
||||||
createPreviews : function () {
|
createPreviews : function () {
|
||||||
|
console.log("createPreviews");
|
||||||
var container = $('#preview-list')[0], previewTile;
|
var container = $('#preview-list')[0], previewTile;
|
||||||
container.innerHTML = "";
|
container.innerHTML = "";
|
||||||
for (var i = 0, l = frameSheet.getFrameCount(); i < l ; i++) {
|
for (var i = 0, l = frameSheet.getFrameCount(); i < l ; i++) {
|
||||||
@ -242,8 +245,7 @@ $.namespace("pskl");
|
|||||||
|
|
||||||
previewTileRoot.addEventListener('click', function(evt) {
|
previewTileRoot.addEventListener('click', function(evt) {
|
||||||
// has not class tile-action:
|
// has not class tile-action:
|
||||||
// TODO: let me know when you want to start using a framework :)
|
if(!evt.target.classList.contains('tile-action')) {
|
||||||
if(!evt.target.className.match(new RegExp('(\\s|^)'+ 'tile-action' +'(\\s|$)'))) {
|
|
||||||
piskel.setActiveFrameAndRedraw(tileNumber);
|
piskel.setActiveFrameAndRedraw(tileNumber);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -256,7 +258,7 @@ $.namespace("pskl");
|
|||||||
piskel.duplicateFrame(tileNumber);
|
piskel.duplicateFrame(tileNumber);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.drawFrameToCanvas(frameSheet.getFrameByIndex(tileNumber), canvasPreview, previewTileCanvasDpi);
|
renderer.render(frameSheet.getFrameByIndex(tileNumber), canvasPreview, previewTileCanvasDpi);
|
||||||
canvasContainer.appendChild(canvasPreview);
|
canvasContainer.appendChild(canvasPreview);
|
||||||
previewTileRoot.appendChild(canvasContainer);
|
previewTileRoot.appendChild(canvasContainer);
|
||||||
previewTileRoot.appendChild(canvasPreviewDuplicateAction);
|
previewTileRoot.appendChild(canvasPreviewDuplicateAction);
|
||||||
@ -275,7 +277,7 @@ $.namespace("pskl");
|
|||||||
},
|
},
|
||||||
|
|
||||||
refreshAnimatedPreview : function () {
|
refreshAnimatedPreview : function () {
|
||||||
piskel.drawFrameToCanvas(frameSheet.getFrameByIndex(animIndex), previewCanvas, previewAnimationCanvasDpi);
|
renderer.render(frameSheet.getFrameByIndex(animIndex), previewCanvas, previewAnimationCanvasDpi);
|
||||||
animIndex++;
|
animIndex++;
|
||||||
if (animIndex == frameSheet.getFrameCount()) {
|
if (animIndex == frameSheet.getFrameCount()) {
|
||||||
animIndex = 0;
|
animIndex = 0;
|
||||||
@ -294,7 +296,7 @@ $.namespace("pskl");
|
|||||||
this.setActiveFrameAndRedraw(frameIndex + 1);
|
this.setActiveFrameAndRedraw(frameIndex + 1);
|
||||||
},
|
},
|
||||||
|
|
||||||
onCanvasMousedown : function (event) {
|
onMousedown : function (event) {
|
||||||
isClicked = true;
|
isClicked = true;
|
||||||
|
|
||||||
if(event.button == 2) { // right click
|
if(event.button == 2) { // right click
|
||||||
@ -306,13 +308,13 @@ $.namespace("pskl");
|
|||||||
spriteCoordinate.col,
|
spriteCoordinate.col,
|
||||||
spriteCoordinate.row,
|
spriteCoordinate.row,
|
||||||
penColor,
|
penColor,
|
||||||
this.drawer
|
this.drawingController
|
||||||
);
|
);
|
||||||
|
|
||||||
$.publish(Events.LOCALSTORAGE_REQUEST);
|
$.publish(Events.LOCALSTORAGE_REQUEST);
|
||||||
},
|
},
|
||||||
|
|
||||||
onCanvasMousemove : function (event) {
|
onMousemove : function (event) {
|
||||||
var currentTime = new Date().getTime();
|
var currentTime = new Date().getTime();
|
||||||
// Throttling of the mousemove event:
|
// Throttling of the mousemove event:
|
||||||
if ((currentTime - previousMousemoveTime) > 40 ) {
|
if ((currentTime - previousMousemoveTime) > 40 ) {
|
||||||
@ -322,7 +324,7 @@ $.namespace("pskl");
|
|||||||
spriteCoordinate.col,
|
spriteCoordinate.col,
|
||||||
spriteCoordinate.row,
|
spriteCoordinate.row,
|
||||||
penColor,
|
penColor,
|
||||||
this.drawer
|
this.drawingController
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO(vincz): Find a way to move that to the model instead of being at the interaction level.
|
// TODO(vincz): Find a way to move that to the model instead of being at the interaction level.
|
||||||
@ -334,7 +336,7 @@ $.namespace("pskl");
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
onDocumentBodyMouseup : function (event) {
|
onMouseup : function (event) {
|
||||||
if(isClicked || isRightClicked) {
|
if(isClicked || isRightClicked) {
|
||||||
// A mouse button was clicked on the drawing canvas before this mouseup event,
|
// A mouse button was clicked on the drawing canvas before this mouseup event,
|
||||||
// the user was probably drawing on the canvas.
|
// the user was probably drawing on the canvas.
|
||||||
@ -358,28 +360,6 @@ $.namespace("pskl");
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
// TODO(vincz/julz): Refactor to make this disappear in a big event-driven redraw loop
|
|
||||||
drawFrameToCanvas: function(frame, canvasElement, dpi) {
|
|
||||||
var color;
|
|
||||||
for(var col = 0, num_col = frame.length; col < num_col; col++) {
|
|
||||||
for(var row = 0, num_row = frame[col].length; row < num_row; row++) {
|
|
||||||
color = frame[col][row];
|
|
||||||
this.drawPixelInCanvas(row, col, color, canvasElement, dpi);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
// TODO(vincz/julz): Refactor to make this disappear in a big event-driven redraw loop
|
|
||||||
drawPixelInCanvas : function (row, col, color, canvas, dpi) {
|
|
||||||
var context = canvas.getContext('2d');
|
|
||||||
if(color == undefined || color == Constants.TRANSPARENT_COLOR) {
|
|
||||||
context.clearRect(col * dpi, row * dpi, dpi, dpi);
|
|
||||||
} else {
|
|
||||||
context.fillStyle = color;
|
|
||||||
context.fillRect(col * dpi, row * dpi, dpi, dpi);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
onCanvasContextMenu : function (event) {
|
onCanvasContextMenu : function (event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
|
@ -46,7 +46,7 @@
|
|||||||
return this.pixels[0].length;
|
return this.pixels[0].length;
|
||||||
};
|
};
|
||||||
|
|
||||||
ns.Frame.prototype.isInFrame = function (col, row) {
|
ns.Frame.prototype.containsPixel = function (col, row) {
|
||||||
return col >= 0 && row >= 0 && col <= this.pixels.length && row <= this.pixels[0].length;
|
return col >= 0 && row >= 0 && col <= this.pixels.length && row <= this.pixels[0].length;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user