feat(ImageShow.svelte-&-utils.js): add canvas support by fabric

BREAKING CHANGE:
This commit is contained in:
songsenand 2022-12-19 07:51:00 +08:00
parent 406123bf8d
commit 4a4a1c0cba
5 changed files with 1508 additions and 35 deletions

1463
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -17,6 +17,7 @@
"@mdi/font": "^7.0.96", "@mdi/font": "^7.0.96",
"bulma": "^0.9.4", "bulma": "^0.9.4",
"fabric": "^5.2.4", "fabric": "^5.2.4",
"jimp": "^0.16.2",
"sass": "^1.56.1", "sass": "^1.56.1",
"uuid": "^9.0.0" "uuid": "^9.0.0"
} }

View File

@ -1,9 +1,15 @@
<script> <script>
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import { onMount } from "svelte"; import { onMount } from "svelte";
import { fabric } from "fabric"; import { fabric } from "fabric";
import Navbar from "./Navbar.svelte"; import Navbar from "./Navbar.svelte";
import { ImageProcessing } from "./utils";
export let url = "test.jpg";
let img_process = new ImageProcessing();
let canvas_dom; let canvas_dom;
let canvas; let canvas;
@ -50,11 +56,17 @@
function resize() { function resize() {
setTimeout(() => { setTimeout(() => {
console.log("test");
canvas_height = window_height - 64; canvas_height = window_height - 64;
canvas_width = (window_width * 13) / 16; canvas_width = (window_width * 13) / 16;
canvas.setWidth(canvas_width); let scale = Math.min(
canvas.setHeight(canvas_height); canvas_height / image.height,
fabric.Image.fromURL("test.jpg", function (oImg) { canvas_width / image.width
);
canvas.setWidth(image.width * scale);
canvas.setHeight(image.height * scale);
image.scale(scale);
/*fabric.Image.fromURL("test.jpg", function (oImg) {
let scale = Math.min( let scale = Math.min(
canvas_height / oImg.height, canvas_height / oImg.height,
canvas_width / oImg.width canvas_width / oImg.width
@ -62,27 +74,32 @@
oImg.scale(scale); oImg.scale(scale);
oImg.set("selectable", false); oImg.set("selectable", false);
canvas.add(oImg); canvas.add(oImg);
}); });*/
}, 50); }, 50);
} }
function init_canvas() { async function init_canvas() {
canvas = new fabric.StaticCanvas(canvas_dom, { let img = new Image();
width: canvas_width, img.src = await img_process.init_image(url);
height: canvas_height, img.onload = function () {
}); setTimeout(() => {
canvas.set("selectable", false); canvas = new fabric.StaticCanvas(canvas_dom);
fabric.Image.fromURL("test.jpg", function (oImg) { image = new fabric.Image(img);
let scale = Math.min( let scale = Math.min(
canvas_height / oImg.height, canvas_height / image.height,
canvas_width / oImg.width canvas_width / image.width
); );
oImg.scale(scale); image.scale(scale);
canvas.add(oImg); canvas.setWidth(image.width * scale);
}); canvas.setHeight(image.height * scale);
canvas.add(image);
console.log(image.getSrc().toString());
}, 50);
};
} }
onMount(() => { onMount(async () => {
canvas_height = window_height - 64; canvas_height = window_height - 64;
canvas_width = (window_width * 13) / 16; canvas_width = (window_width * 13) / 16;
init_canvas(); init_canvas();
@ -124,7 +141,7 @@
{/each} {/each}
</aside> </aside>
</div> </div>
<div class="column"> <div class="column" style="text-align:center">
<canvas <canvas
bind:this={canvas_dom} bind:this={canvas_dom}
width={canvas_width} width={canvas_width}

View File

@ -1,4 +1,5 @@
import procedure from "../assets/Procedure.json"; import procedure from "../assets/Procedure.json";
import jimp from "jimp";
export function Operation(step) { export function Operation(step) {
this.step = step; this.step = step;
@ -26,3 +27,16 @@ export function Operation(step) {
return procedure.find((e) => e.id === this.step).color; return procedure.find((e) => e.id === this.step).color;
}; };
} }
export function ImageProcessing() {
this.init_image = async (url) => {
const image = await jimp.read(url);
return image.getBase64Async(jimp.MIME_PNG);
};
this.rotate = async (url, angle) => {
const image = await jimp.read(url);
image.rotate(angle);
return image.getBase64Async(jimp.MIME_PNG);
};
}

View File

@ -1,7 +1,7 @@
import { defineConfig } from 'vite' import { defineConfig } from "vite";
import { svelte } from '@sveltejs/vite-plugin-svelte' import { svelte } from "@sveltejs/vite-plugin-svelte";
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ export default defineConfig({
plugins: [svelte()] plugins: [svelte()],
}) });