项目作者: gillespiecd

项目描述 :
Light-weight canvas photo editor
高级语言: CSS
项目地址: git://github.com/gillespiecd/LiteRoomJS.git
创建时间: 2017-05-28T18:46:12Z
项目社区:https://github.com/gillespiecd/LiteRoomJS

开源协议:

下载


LiteRoomJS

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.

Technology

  • HTML5 canvas to handle image state, filters, and display

    1. ctx.filter handles filtering logic
    2. ctx.rotate and ctx.translate to re-arrange canvas when rotating
  • Vanilla JS classes

    1. main.js - initializes program with image (default or uploaded)
    2. canvas.js - holds state of canvas, stores filter values, draws image
    3. filter.js - callback functions to update image with new filter

Features

Upload image from computer

Modify image with filters (grayscale, saturation, contrast, etc.)

Download edited image locally

Image rotation

Code Examples

Sliders

  1. applySlider(filter) {
  2. const self = this;
  3. const filterSlider = document.getElementById(filter);
  4. filterSlider.onchange = function(e) {
  5. self.canvas.drawCanvas(`${filter}(${e.target.value}%)`);
  6. };
  7. }
  8. grayscaleSlider() {
  9. this.applySlider('grayscale');
  10. }
  11. sepiaSlider() {
  12. this.applySlider('sepia');
  13. }

Rotation

  1. rotate() {
  2. const rotateButton = document.getElementById("rotate-image");
  3. let newImage;
  4. rotateButton.onclick = () => {
  5. newImage = new Image();
  6. newImage.src = this.canvas.toDataURL();
  7. newImage.onload = () => {
  8. const oldWidth = this.canvas.width;
  9. this.canvas.width = this.canvas.height;
  10. this.canvas.height = oldWidth;
  11. this.ctx.translate(this.canvas.width, this.canvas.height / this.canvas.width);
  12. this.ctx.rotate(Math.PI / 2);
  13. this.ctx.drawImage(newImage, 0, 0);
  14. };
  15. };
  16. }

Filters

Stored as hashmap — updated with each draw method

  1. updateFilters(filter) {
  2. const filterName = filter.match(/\w+/)[0];
  3. const filterValue = filter.match(/\d+/)[0];
  4. this.filters[filterName] = filterValue;
  5. let filterString = "";
  6. for (var key in this.filters) {
  7. filterString += ` ${key}(${this.filters[key]}%)`;
  8. }
  9. this.ctx.filter = filterString;
  10. }