soiz commited on
Commit
e3bf736
·
verified ·
1 Parent(s): a13f8e3

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +115 -97
index.html CHANGED
@@ -1,104 +1,122 @@
1
  <!DOCTYPE html>
2
  <html lang="ja">
3
  <head>
4
- <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>モザイク画像の3D変換</title>
7
- <style>
8
- body { margin: 0; overflow: hidden; }
9
- canvas { display: block; }
10
- #upload { position: fixed; top: 10px; left: 10px; z-index: 10; }
11
- </style>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  </head>
13
  <body>
14
- <input type="file" id="upload" accept="image/*" />
15
- <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
16
- <script>
17
- // 基本的なThree.jsのセットアップ
18
- let scene, camera, renderer, controls;
19
- const cubes = [];
20
- const pixelSize = 5; // ピクセル1つあたりの立方体サイズ
21
-
22
- function init() {
23
- scene = new THREE.Scene();
24
- camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
25
- camera.position.z = 300;
26
-
27
- renderer = new THREE.WebGLRenderer();
28
- renderer.setSize(window.innerWidth, window.innerHeight);
29
- document.body.appendChild(renderer.domElement);
30
-
31
- animate();
32
- }
33
-
34
- function animate() {
35
- requestAnimationFrame(animate);
36
- cubes.forEach(cube => {
37
- // 各立方体が中心に集まるように位置を調整
38
- cube.position.x += (0 - cube.position.x) * 0.02;
39
- cube.position.y += (0 - cube.position.y) * 0.02;
40
- cube.position.z += (0 - cube.position.z) * 0.02;
41
- });
42
- camera.rotation.y += 0.01; // カメラの回転
43
- renderer.render(scene, camera);
44
- }
45
-
46
- // モザイク画像を生成する関数
47
- function createMosaicImage(image) {
48
- const canvas = document.createElement("canvas");
49
- const ctx = canvas.getContext("2d");
50
- const width = 50; // モザイクの幅
51
- const height = 50; // モザイクの高さ
52
- canvas.width = width;
53
- canvas.height = height;
54
-
55
- // 画像を縮小してモザイク化
56
- ctx.drawImage(image, 0, 0, width, height);
57
- const imageData = ctx.getImageData(0, 0, width, height).data;
58
-
59
- // 各ピクセルから立方体を作成
60
- for (let y = 0; y < height; y++) {
61
- for (let x = 0; x < width; x++) {
62
- const i = (y * width + x) * 4;
63
- const r = imageData[i];
64
- const g = imageData[i + 1];
65
- const b = imageData[i + 2];
66
- const color = new THREE.Color(`rgb(${r},${g},${b})`);
67
-
68
- const geometry = new THREE.BoxGeometry(pixelSize, pixelSize, pixelSize);
69
- const material = new THREE.MeshBasicMaterial({ color });
70
- const cube = new THREE.Mesh(geometry, material);
71
-
72
- // 初期位置をランダムに設定
73
- cube.position.x = Math.random() * 600 - 300;
74
- cube.position.y = Math.random() * 600 - 300;
75
- cube.position.z = Math.random() * 600 - 300;
76
-
77
- // 各ピクセルの位置に立方体を配置
78
- const targetX = (x - width / 2) * pixelSize;
79
- const targetY = (y - height / 2) * pixelSize;
80
- cube.userData = { targetX, targetY, targetZ: 0 };
81
-
82
- cubes.push(cube);
83
- scene.add(cube);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>