Skip to content

Commit

Permalink
readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
b4rtaz committed Oct 15, 2023
1 parent 5d62aa4 commit 1d214d2
Show file tree
Hide file tree
Showing 29 changed files with 464 additions and 77 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: examples
on:
push:
branches:
- main
jobs:
build:
name: Build
runs-on: ${{matrix.os}}
strategy:
matrix:
os:
- ubuntu-latest
permissions:
contents: read
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Checkout Repo
uses: actions/checkout@v3
- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: 18
- uses: pnpm/action-setup@v2
name: Install pnpm
with:
version: 8
run_install: false
- name: Install
run: pnpm install
- name: Build
run: pnpm build
- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
path: demos
- name: Deploy
id: deployment
uses: actions/deploy-pages@v1
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2023 N4NO.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Mini Canvas Editor

Canvas editor component for JavaScript application. Works with any front-end framework. Easy to integrate and use. Well-known graphical interface. Edit images, draw shapes, add texts and more. Gzipped size less than 60 KB. Uses Fabric.js internally.

Main use cases:

* resize image,
* crop image,
* create a template, render it on the front-end and the back-end (Node.js only),
* create inpainting mask.

Online Examples:

* [🎨 Inpainting Mask](demos/webpack-app/public/inpainting-mask.html)
* [🎬 Template Creator](demos/webpack-app/public/template-creator.html)

## 💡 License

This project is released under the MIT license.
7 changes: 7 additions & 0 deletions canvas/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Mini Canvas

This package contains the core features for [Mini Canvas Editor](https://github.com/b4rtaz/mini-canvas-editor).

## 💡 License

This project is released under the MIT license.
26 changes: 25 additions & 1 deletion canvas/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "mini-canvas",
"description": "Core library for Mini Canvas Editor.",
"version": "0.0.1",
"license": "MIT",
"type": "module",
Expand Down Expand Up @@ -44,6 +45,15 @@
"publishConfig": {
"registry": "https://registry.npmjs.org/"
},
"repository": {
"type": "git",
"url": "https://github.com/b4rtaz/mini-canvas-editor.git"
},
"author": {
"name": "N4NO",
"url": "https://n4no.com/"
},
"homepage": "https://n4no.com/",
"scripts": {
"clean": "rm -rf lib && rm -rf node_modules/.cache/rollup-plugin-typescript2",
"build": "pnpm clean && rollup -c",
Expand All @@ -66,5 +76,19 @@
"@typescript-eslint/eslint-plugin": "^6.7.5",
"@typescript-eslint/parser": "^6.7.5",
"eslint": "^8.51.0"
}
},
"keywords": [
"canvas",
"editor",
"image",
"image editor",
"photo editor",
"photo",
"javascript image editor",
"paint",
"js paint",
"image crop",
"image resize",
"inpainting"
]
}
4 changes: 4 additions & 0 deletions demos/webpack-app/public/assets/background-pattern.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions demos/webpack-app/public/inpainting-mask.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>🎨 Inpainting Mask - Mini Canvas Editor</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<link rel="icon" href="./assets/favicon.ico" />
<style>
html,
body {
margin: 0;
padding: 0;
font: 14px/1.5em Arial, Verdana, sans-serif;
min-height: 100%;
background: url('./assets//background-pattern.svg');
}
#placeholder {
width: 100%;
height: 50vh;
}
p, .debug {
text-align: center;
margin: 0;
padding: 10px 0;
}
.preview-image {
margin: 0 10px;
width: 30vw;
height: auto;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);
}
</style>
<script src="./builds/inpainting-mask.js" defer></script>
</head>
<body>
<div id="placeholder"></div>

<div class="debug">
<img id="previewImage" class="preview-image" />
<img id="previewMaskImage" class="preview-image" />
</div>

<p>
<a href="">Mini Canvas Editor</a>
</p>
</body>
</html>
4 changes: 2 additions & 2 deletions demos/webpack-app/public/template-creator.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Template Creator - Mini Canvas Editor</title>
<title>🎬 Template Creator - Mini Canvas Editor</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<link rel="icon" href="./assets/favicon.ico" />
<style>
Expand All @@ -11,7 +11,7 @@
margin: 0;
padding: 0;
font: 14px/1.5em Arial, Verdana, sans-serif;
background: linear-gradient(0deg, #fff 0%, #80a30b 100%);
background: url('./assets//background-pattern.svg');
min-height: 100%;
}
#placeholder {
Expand Down
89 changes: 89 additions & 0 deletions demos/webpack-app/src/inpainting-mask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Editor, EditorMode } from 'mini-canvas-editor';
import { MceImage } from 'mini-canvas';

import 'mini-canvas-editor/css/editor.css';

export class App {
public static preload() {
const preloader = new Image();
preloader.src = './assets/cat.jpg';
preloader.onload = () => {
App.create(preloader);
};
}

public static create(image: HTMLImageElement) {
const placeholder = document.getElementById('placeholder') as HTMLElement;
const previewImage = document.getElementById('previewImage') as HTMLImageElement;
const previewMaskImage = document.getElementById('previewMaskImage') as HTMLImageElement;

const editor = Editor.createFromImage(placeholder, image, false, {
initialMode: EditorMode.brush,
brush: {
brushColor: '#ff0000',
brushSize: 20
},
rect: {
fillColor: '#ff0000'
},
image: false,
textbox: false
});
const app = new App(editor, previewImage, previewMaskImage);
editor.onChanged.subscribe(app.reloadPreview);
app.reloadPreview();
return app;
}

private constructor(
private readonly editor: Editor,
private readonly previewImage: HTMLImageElement,
private readonly previewMaskImage: HTMLImageElement
) {}

private readonly reloadPreview = async () => {
const objects = this.editor.getWorkspaceObjects();
const image = objects.find(o => o instanceof MceImage) as MceImage;
if (!image || !image.visible) {
this.previewImage.src = '';
this.previewMaskImage.src = '';
return;
}

const [maskCanvas, maskCanvasContext] = createMemoryCanvas(this.editor.getWidth(), this.editor.getHeight());
objects.forEach(obj => {
if (obj !== image) {
obj.render(maskCanvasContext);
}
});

const [imageCanvas, imageCanvasContext] = createMemoryCanvas(this.editor.getWidth(), this.editor.getHeight());
image.render(imageCanvasContext);

applyMask(this.editor.getWidth(), this.editor.getHeight(), imageCanvasContext, maskCanvasContext);

this.previewImage.src = imageCanvas.toDataURL();
this.previewMaskImage.src = maskCanvas.toDataURL();
};
}

document.addEventListener('DOMContentLoaded', App.preload, false);

function createMemoryCanvas(width: number, height: number): [HTMLCanvasElement, CanvasRenderingContext2D] {
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const canvasContext = canvas.getContext('2d') as CanvasRenderingContext2D;
canvasContext.fillStyle = '#ffffff00';
canvasContext.fillRect(0, 0, width, height);
return [canvas, canvasContext];
}

function applyMask(width: number, height: number, target: CanvasRenderingContext2D, mask: CanvasRenderingContext2D) {
const imageCanvasData = target.getImageData(0, 0, width, height);
const maskCanvasData = mask.getImageData(0, 0, width, height);
for (let i = 0; i < imageCanvasData.data.length; i += 4) {
imageCanvasData.data[i + 3] = 255 - maskCanvasData.data[i + 3];
}
target.putImageData(imageCanvasData, 0, 0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ export class App {
const placeholder = document.getElementById('placeholder') as HTMLElement;
const preview = document.getElementById('preview') as HTMLImageElement;

const editor = Editor.create(placeholder, {
width: 400,
height: 300
});
const editor = Editor.createBlank(placeholder, 400, 300, {});
editor.add(
new MceRect({
fill: '#ffffff',
Expand All @@ -38,15 +35,15 @@ export class App {
fontSize: 40,
left: 140,
top: 52,
width: 300
width: 240
})
);
editor.add(
new MceTextbox('Time: $time', {
fontSize: 20,
left: 140,
top: 120,
width: 300,
width: 240,
fill: '#8c8c8c'
})
);
Expand Down
5 changes: 3 additions & 2 deletions demos/webpack-app/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const path = require('path');

function bundle(name) {
return {
entry: `./src/${name}/app.ts`,
entry: `./src/${name}.ts`,
cache: false,
module: {
rules: [
Expand All @@ -28,5 +28,6 @@ function bundle(name) {
}

module.exports = [
bundle('template-creator')
bundle('template-creator'),
bundle('inpainting-mask'),
];
2 changes: 2 additions & 0 deletions editor/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/lib
LICENSE
README.md
28 changes: 26 additions & 2 deletions editor/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "mini-canvas-editor",
"description": "Canvas editor component for JavaScript application. Easy to integrate and use.",
"version": "0.0.1",
"type": "module",
"main": "./lib/esm/index.js",
Expand All @@ -20,7 +21,6 @@
"default": "./css/editor.css"
}
},
"homepage": "https://nocode-js.com/",
"license": "MIT",
"files": [
"css/",
Expand All @@ -30,7 +30,17 @@
"publishConfig": {
"registry": "https://registry.npmjs.org/"
},
"repository": {
"type": "git",
"url": "https://github.com/b4rtaz/mini-canvas-editor.git"
},
"author": {
"name": "N4NO",
"url": "https://n4no.com/"
},
"homepage": "https://n4no.com/",
"scripts": {
"prepare": "cp ../LICENSE LICENSE && cp ../README.md README.md",
"clean": "rm -rf lib && rm -rf build && rm -rf dist && rm -rf node_modules/.cache/rollup-plugin-typescript2",
"start": "rollup -c --watch",
"build": "pnpm clean && rollup -c",
Expand Down Expand Up @@ -63,5 +73,19 @@
"karma-spec-reporter": "^0.0.36",
"karma-typescript": "^5.5.4",
"karma-typescript-es6-transform": "^5.5.4"
}
},
"keywords": [
"canvas",
"editor",
"image",
"image editor",
"photo editor",
"photo",
"javascript image editor",
"paint",
"js paint",
"image crop",
"image resize",
"inpainting"
]
}
Loading

0 comments on commit 1d214d2

Please sign in to comment.