Update index.html
Browse files- index.html +103 -18
index.html
CHANGED
|
@@ -1,19 +1,104 @@
|
|
| 1 |
-
<!
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
</html>
|
|
|
|
| 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>
|