Update index.html
Browse files- index.html +115 -97
index.html
CHANGED
@@ -1,104 +1,122 @@
|
|
1 |
<!DOCTYPE html>
|
2 |
<html lang="ja">
|
3 |
<head>
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
</head>
|
13 |
<body>
|
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 |
-
document.getElementById("upload").addEventListener("change", (event) => {
|
90 |
-
const file = event.target.files[0];
|
91 |
-
const reader = new FileReader();
|
92 |
-
reader.onload = (e) => {
|
93 |
-
const image = new Image();
|
94 |
-
image.onload = () => createMosaicImage(image);
|
95 |
-
image.src = e.target.result;
|
96 |
-
};
|
97 |
-
reader.readAsDataURL(file);
|
98 |
-
});
|
99 |
-
|
100 |
-
// 初期化
|
101 |
-
init();
|
102 |
-
</script>
|
103 |
</body>
|
104 |
-
</html>
|
|
|
1 |
<!DOCTYPE html>
|
2 |
<html lang="ja">
|
3 |
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<title>画像モザイク&3Dアニメーション</title>
|
6 |
+
<style>
|
7 |
+
body {
|
8 |
+
display: flex;
|
9 |
+
flex-direction: column;
|
10 |
+
align-items: center;
|
11 |
+
background-color: #222;
|
12 |
+
color: #fff;
|
13 |
+
font-family: Arial, sans-serif;
|
14 |
+
}
|
15 |
+
canvas {
|
16 |
+
display: block;
|
17 |
+
margin-top: 20px;
|
18 |
+
}
|
19 |
+
#scene-container {
|
20 |
+
width: 80vw;
|
21 |
+
height: 80vh;
|
22 |
+
}
|
23 |
+
#uploadForm {
|
24 |
+
margin-top: 20px;
|
25 |
+
}
|
26 |
+
</style>
|
27 |
</head>
|
28 |
<body>
|
29 |
+
<h1>画像のモザイク処理と3Dアニメーション</h1>
|
30 |
+
<form id="uploadForm">
|
31 |
+
<input type="file" id="imageInput" accept="image/*">
|
32 |
+
<button type="button" onclick="processImage()">画像を処理</button>
|
33 |
+
</form>
|
34 |
+
<canvas id="canvas" style="display: none;"></canvas>
|
35 |
+
<div id="scene-container"></div>
|
36 |
+
|
37 |
+
<!-- Three.js ライブラリ -->
|
38 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
|
39 |
+
<script>
|
40 |
+
const canvas = document.getElementById('canvas');
|
41 |
+
const ctx = canvas.getContext('2d');
|
42 |
+
let scene, camera, renderer;
|
43 |
+
const cubeSize = 5; // 立方体のサイズ
|
44 |
+
let cubes = []; // 立方体を格納する配列
|
45 |
+
|
46 |
+
function processImage() {
|
47 |
+
const fileInput = document.getElementById('imageInput');
|
48 |
+
const file = fileInput.files[0];
|
49 |
+
if (!file) return;
|
50 |
+
|
51 |
+
const img = new Image();
|
52 |
+
img.onload = () => {
|
53 |
+
const scale = 0.1; // モザイクの縮小スケール
|
54 |
+
canvas.width = img.width * scale;
|
55 |
+
canvas.height = img.height * scale;
|
56 |
+
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
|
57 |
+
|
58 |
+
create3DScene();
|
59 |
+
};
|
60 |
+
img.src = URL.createObjectURL(file);
|
61 |
+
}
|
62 |
+
|
63 |
+
function create3DScene() {
|
64 |
+
// Three.jsの基本設定
|
65 |
+
scene = new THREE.Scene();
|
66 |
+
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
|
67 |
+
camera.position.z = 300;
|
68 |
+
|
69 |
+
renderer = new THREE.WebGLRenderer();
|
70 |
+
renderer.setSize(window.innerWidth, window.innerHeight);
|
71 |
+
document.getElementById('scene-container').appendChild(renderer.domElement);
|
72 |
+
|
73 |
+
createCubesFromImage();
|
74 |
+
|
75 |
+
animate(); // アニメーション開始
|
76 |
+
}
|
77 |
+
|
78 |
+
function createCubesFromImage() {
|
79 |
+
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
|
80 |
+
|
81 |
+
for (let y = 0; y < canvas.height; y++) {
|
82 |
+
for (let x = 0; x < canvas.width; x++) {
|
83 |
+
const index = (y * canvas.width + x) * 4;
|
84 |
+
const r = imageData[index];
|
85 |
+
const g = imageData[index + 1];
|
86 |
+
const b = imageData[index + 2];
|
87 |
+
const color = new THREE.Color(`rgb(${r},${g},${b})`);
|
88 |
+
|
89 |
+
const geometry = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
|
90 |
+
const material = new THREE.MeshBasicMaterial({ color: color });
|
91 |
+
const cube = new THREE.Mesh(geometry, material);
|
92 |
+
|
93 |
+
cube.position.set(
|
94 |
+
(x - canvas.width / 2) * cubeSize * 1.2,
|
95 |
+
(y - canvas.height / 2) * cubeSize * 1.2,
|
96 |
+
Math.random() * 200 - 100 // 初期位置のばらつきを与える
|
97 |
+
);
|
98 |
+
scene.add(cube);
|
99 |
+
cubes.push(cube);
|
100 |
+
}
|
101 |
+
}
|
102 |
+
}
|
103 |
+
|
104 |
+
function animate() {
|
105 |
+
requestAnimationFrame(animate);
|
106 |
+
|
107 |
+
// 各立方体をターゲット位置へ移動させる
|
108 |
+
cubes.forEach((cube, index) => {
|
109 |
+
const targetX = ((index % canvas.width) - canvas.width / 2) * cubeSize * 1.2;
|
110 |
+
const targetY = (Math.floor(index / canvas.width) - canvas.height / 2) * cubeSize * 1.2;
|
111 |
+
const targetZ = 0;
|
112 |
+
|
113 |
+
cube.position.x += (targetX - cube.position.x) * 0.05;
|
114 |
+
cube.position.y += (targetY - cube.position.y) * 0.05;
|
115 |
+
cube.position.z += (targetZ - cube.position.z) * 0.05;
|
116 |
+
});
|
117 |
+
|
118 |
+
renderer.render(scene, camera);
|
119 |
}
|
120 |
+
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
</body>
|
122 |
+
</html>
|