konva/test/sandbox.html

103 lines
3.1 KiB
HTML
Raw Normal View History

2021-05-04 06:09:18 +08:00
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>KonvaJS Sandbox</title>
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=1.0, minimum-scale=1.0, maximum-scale=1.0"
/>
<style>
body {
margin: 0;
2022-08-05 23:35:43 +08:00
width: 100vw;
height: 100vh;
2021-05-04 06:09:18 +08:00
}
</style>
<!-- <script src="https://cdn.rawgit.com/hammerjs/touchemulator/master/touch-emulator.js"></script> -->
<script>
2021-08-04 16:28:00 +08:00
// TouchEmulator();
2021-05-04 06:09:18 +08:00
</script>
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.7/hammer.js"></script> -->
<!-- <script src="https://cdn.rawgit.com/hammerjs/touchemulator/master/touch-emulator.js"></script> -->
<!-- <script src="./hammer.js"></script> -->
2022-08-05 23:35:43 +08:00
<!-- <script src="https://unpkg.com/fabric@5.2.1/dist/fabric.js"></script> -->
2021-05-04 06:09:18 +08:00
</head>
<body>
2022-08-05 23:35:43 +08:00
<div id="container" style="background-color: bisque"></div>
2021-10-22 23:09:34 +08:00
<script type="module">
import Konva from '../src/index.ts';
2022-03-22 02:49:55 +08:00
2023-07-27 01:10:00 +08:00
const config = {
canvas: { width: window.innerWidth, height: window.innerHeight },
filter: { pixelSize: 26 },
image: { naturalWidth: 500, naturalHeight: 500, scale: 1 },
imageCropped: { width: 150, height: 150 },
shapeReference: { colorOdd: 'white', colorEven: 'black' },
};
const image = document.createElement('img');
image.crossOrigin = 'anonymous';
image.src = `https://placekitten.com/${config.image.naturalWidth}/${config.image.naturalHeight}`;
var stage = new Konva.Stage({
2022-08-05 23:35:43 +08:00
container: 'container',
2023-07-27 01:10:00 +08:00
width: config.canvas.width,
height: config.canvas.height,
2022-08-05 23:35:43 +08:00
});
2023-07-27 01:10:00 +08:00
var layer = new Konva.Layer();
2022-08-05 23:35:43 +08:00
stage.add(layer);
2023-07-27 01:10:00 +08:00
image.onload = async function () {
// await image.decode(); // used to dynamically determine natural dimensions - not needed, in config
var imageOriginal = new Konva.Image({
x: 0,
y: 0,
scaleX: config.image.scale,
scaleY: config.image.scale,
image,
width: config.image.naturalWidth,
height: config.image.naturalHeight,
2023-04-17 22:55:10 +08:00
});
2023-07-27 01:10:00 +08:00
var imageFiltered = new Konva.Image({
cropX: 0,
cropY: 0,
cropWidth: config.imageCropped.width,
cropHeight: config.imageCropped.height,
x: 0,
y: 0,
scaleX: config.image.scale,
scaleY: config.image.scale,
image,
width: config.imageCropped.width,
height: config.imageCropped.height,
pixelSize: config.filter.pixelSize,
});
imageFiltered.cache();
imageFiltered.filters([Konva.Filters.Pixelate]);
var rect = new Konva.Image({
x: 0,
y: 0,
scaleX: config.image.scale,
scaleY: config.image.scale,
width: config.imageCropped.width,
height: config.imageCropped.height,
stroke: 'red',
strokeWidth: 0.5,
});
// add the shapes to the layer
layer.add(imageOriginal, imageFiltered, rect);
};
2021-10-22 23:09:34 +08:00
</script>
2021-05-04 06:09:18 +08:00
</body>
</html>