-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathresize-image.html
153 lines (132 loc) · 5.35 KB
/
resize-image.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!--
speedy-vision.js
GPU-accelerated Computer Vision for JavaScript
Copyright 2020-2024 Alexandre Martins <alemartf(at)gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
resize-image.html
Resize an image
-->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="speedy-vision.js: GPU-accelerated Computer Vision for JavaScript">
<meta name="author" content="Alexandre Martins">
<title>Resize an image</title>
<link href="demo-base.css" rel="stylesheet">
<script src="demo-base.js"></script>
<script src="../dist/speedy-vision.js"></script>
</head>
<body>
<h1>Resize an image</h1>
<form autocomplete="off">
<div>
<label for="scale-x">Width</label>
<input type="range" min="0.1" max="5.0" value="1.0" step="0.1" id="scale-x">
<span id="scale-x-percentage">100%</span>
<span> </span>
</div>
<div>
<label for="scale-y">Height</label>
<input type="range" min="0.1" max="5.0" value="1.0" step="0.1" id="scale-y">
<span id="scale-y-percentage">100%</span>
<span> </span>
</div>
<div>
<label for="keep-aspect-ratio">Keep aspect ratio</label>
<input type="checkbox" id="keep-aspect-ratio" checked>
</div>
<div>
<select id="interpolation-method">
<option value="bilinear" selected>Bilinear interpolation</option>
<option value="nearest">Nearest neighbors</option>
</select>
</div>
</form>
<div>
<canvas id="canvas-demo"></canvas>
</div>
<img id="image" src="../assets/speedy-wall.jpg" title="Image by Bride of Frankenstein (CC-BY)" hidden>
<script>
window.onload = async function()
{
// form controls
const scaleX = document.getElementById('scale-x');
const scaleY = document.getElementById('scale-y');
const scaleXPercentage = document.getElementById('scale-x-percentage');
const scaleYPercentage = document.getElementById('scale-y-percentage');
const keepAspectRatio = document.getElementById('keep-aspect-ratio');
const interpolationMethod = document.getElementById('interpolation-method');
const format = x => (x >= 100 ? x : (x >= 10 ? '0' + x : '00' + x)) + '%';
scaleX.oninput = () => {
scaleXPercentage.innerText = format(Math.round(scaleX.value * 100));
if(keepAspectRatio.checked) {
scaleY.value = scaleX.value;
scaleYPercentage.innerText = format(Math.round(scaleY.value * 100));
}
render();
};
scaleY.oninput = () => {
scaleYPercentage.innerText = format(Math.round(scaleY.value * 100));
if(keepAspectRatio.checked) {
scaleX.value = scaleY.value;
scaleXPercentage.innerText = format(Math.round(scaleX.value * 100));
}
render();
};
keepAspectRatio.onchange = () => {
if(keepAspectRatio.checked) {
scaleY.value = scaleX.value;
scaleYPercentage.innerText = format(Math.round(scaleY.value * 100));
}
render();
}
interpolationMethod.onchange = render;
/*
This is the pipeline:
Image ---> Resize ---> Image
Source Sink
*/
// load an image
const img = document.getElementById('image');
const media = await Speedy.load(img);
// create the pipeline
const pipeline = Speedy.Pipeline();
const source = Speedy.Image.Source();
const sink = Speedy.Image.Sink();
const resize = Speedy.Transform.Resize();
source.media = media;
resize.size = Speedy.Size(0,0);
source.output().connectTo(resize.input());
resize.output().connectTo(sink.input());
pipeline.init(source, sink, resize);
// display the result
const canvas = setupCanvas('canvas-demo', media.width, media.height, img.title);
async function render()
{
resize.scale = Speedy.Vector2(scaleX.value, scaleY.value);
resize.method = interpolationMethod.value;
const { image } = await pipeline.run();
canvas.width = image.width;
canvas.height = image.height;
renderMedia(image, canvas);
}
render();
}
</script>
<section id="speedy-vision">
<span>Powered by <a href="https://github.com/alemart/speedy-vision" target="_blank">speedy-vision.js</a></span>
<a href="https://github.com/sponsors/alemart" target="_blank" class="button">
<img alt="GitHub Sponsors" src="https://img.shields.io/github/sponsors/alemart?style=for-the-badge&logo=github&label=Sponsor&labelColor=royalblue&color=gold">
</a>
</section>
</body>
</html>