Light-weight canvas photo editor
LiteRoomJS is a lightweight canvas photo editor for web-based editing
of photographs.
Users can upload an image, edit it, and then export an edited version.
HTML5 canvas to handle image state, filters, and display
ctx.filter
handles filtering logicctx.rotate
and ctx.translate
to re-arrange canvas when rotatingVanilla JS classes
main.js
- initializes program with image (default or uploaded)canvas.js
- holds state of canvas, stores filter values, draws imagefilter.js
- callback functions to update image with new filterUpload image from computer
Modify image with filters (grayscale, saturation, contrast, etc.)
Download edited image locally
Image rotation
applySlider(filter) {
const self = this;
const filterSlider = document.getElementById(filter);
filterSlider.onchange = function(e) {
self.canvas.drawCanvas(`${filter}(${e.target.value}%)`);
};
}
grayscaleSlider() {
this.applySlider('grayscale');
}
sepiaSlider() {
this.applySlider('sepia');
}
rotate() {
const rotateButton = document.getElementById("rotate-image");
let newImage;
rotateButton.onclick = () => {
newImage = new Image();
newImage.src = this.canvas.toDataURL();
newImage.onload = () => {
const oldWidth = this.canvas.width;
this.canvas.width = this.canvas.height;
this.canvas.height = oldWidth;
this.ctx.translate(this.canvas.width, this.canvas.height / this.canvas.width);
this.ctx.rotate(Math.PI / 2);
this.ctx.drawImage(newImage, 0, 0);
};
};
}
Stored as hashmap — updated with each draw method
updateFilters(filter) {
const filterName = filter.match(/\w+/)[0];
const filterValue = filter.match(/\d+/)[0];
this.filters[filterName] = filterValue;
let filterString = "";
for (var key in this.filters) {
filterString += ` ${key}(${this.filters[key]}%)`;
}
this.ctx.filter = filterString;
}