-
![]()
-
![]()
+
+
![]()
+
+
From 4a857208fc32eabc5dd08fed4830962989db6714 Mon Sep 17 00:00:00 2001
From: neverbiasu <1751162157@qq.com>
Date: Wed, 3 Jul 2024 17:45:47 +0800
Subject: [PATCH 5/6] update the codes of image transform
---
fixtures/css/image-verification.ts | 69 +++++++---------------------
fixtures/css/image-verification.xsml | 13 +++++-
2 files changed, 28 insertions(+), 54 deletions(-)
diff --git a/fixtures/css/image-verification.ts b/fixtures/css/image-verification.ts
index 3f370f0..dfa1fd6 100644
--- a/fixtures/css/image-verification.ts
+++ b/fixtures/css/image-verification.ts
@@ -1,56 +1,33 @@
import splattingPic from '../textures/splatting.jpg'
+const plane = spatialDocument.querySelector('plane') as any;
+const panel = plane.shadowRoot;
+const div = panel.querySelector('div');
+const images = div.querySelectorAll('img');
+const img1 = images[0];
+const img2 = images[1];
+const p = div.querySelector('p');
+
spatialDocument.addEventListener('spaceReady', () => {
- const plane = spatialDocument.querySelector('plane') as any;
createImageBitmap(new Blob([splattingPic], { type: 'image/jpeg' })).then(async (bitmap) => {
const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
const ctx = canvas.getContext('2d');
if (ctx == null) {
return;
}
-
- const panel = plane.shadowRoot;
- const div = panel.querySelector('div');
- const images = div.querySelectorAll('img');
- const img1 = images[0];
- const img2 = images[1];
-
const leftImageData = getShapeFromImage(bitmap, 'rect');
const rightImageData = cutShapeFromImage(bitmap, 'rect');
const leftImageURL = await convertImageDataToDataURL(leftImageData);
const rightImageURL = await convertImageDataToDataURL(rightImageData);
- // console.log('leftImageURL', leftImageURL);
- // console.log('rightImageURL', rightImageURL);
img1.src = leftImageURL;
img2.src = rightImageURL;
-
- // console.log('img1.style', img1.style);
-
- // const imageData = rightImageData;
-
- // const imageData = combineImages(leftImageData, rightImageData);
-
- // console.log('imageData', imageData);
- // const planeTexture = new BABYLON.RawTexture(
- // imageData.data,
- // imageData.width,
- // imageData.height,
- // BABYLON.Engine.TEXTUREFORMAT_RGBA,
- // spatialDocument.scene,
- // false,
- // false,
- // BABYLON.Texture.TRILINEAR_SAMPLINGMODE
- // );
-
- // const mat = new BABYLON.StandardMaterial('plane', spatialDocument.scene);
- // mat.diffuseTexture = planeTexture;
- // mat.diffuseColor = new BABYLON.Color3(255, 255, 255);
- // plane.asNativeType().material = mat;
});
})
+moveImageWithRay();
+
async function convertImageDataToDataURL(imageData: ImageData): Promise
{
const canvas = new OffscreenCanvas(imageData.width, imageData.height);
const ctx = canvas.getContext('2d');
@@ -71,20 +48,14 @@ async function convertImageDataToDataURL(imageData: ImageData): Promise
return dataURL;
}
-function moveImageWithRay(img) {
- const originMouseX = img.style.originX;
- img.addEventListener('mouseclick', (event) => {
- const currentMouseX = event.x;
- if (currentMouseX < originMouseX + 20 && currentMouseX > originMouseX - 20) {
-
+function moveImageWithRay() {
+ div.addEventListener('mousemove', (event) => {
+ const mouseX = Math.round(event.x);
+ const gapX = mouseX - 128;
+ img1.style.transform = `translateX(${gapX}px)`;
+ if (gapX >= 763 && gapX <= 773) {
+ p.textContent = 'Success!';
}
- const gapX = originMouseX - currentMouseX;
- img.style.transform = `translateX(${gapX}px)`;
- })
- img.addEventListener('mouseLeave', (event) => {
- const currentMouseX = event.x;
- const gapX = originMouseX - currentMouseX;
- img.style.transform = `translateX(${gapX}px)`;
})
}
@@ -93,7 +64,6 @@ function cutShapeFromImage(image, shape: string) {
const ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0);
- // 设置合成操作为'destination-out',这样后续的绘制操作会从已有的内容中删除像素
ctx.globalCompositeOperation = 'destination-out';
if (shape === 'rect') {
@@ -109,17 +79,12 @@ function getShapeFromImage(image, shape: string) {
const canvas = new OffscreenCanvas(image.width, image.height);
const ctx = canvas.getContext('2d');
- // ctx.fillStyle = 'black';
- ctx.fillRect(0, 0, image.width, image.height);
-
- // 定义剪辑区域
if (shape === 'rect') {
ctx.beginPath();
ctx.rect(1/4 * image.width, 1/4 * image.height, 1/2 * image.width, 1/2 * image.height);
ctx.clip();
}
- // 在剪辑区域内绘制图像
ctx.drawImage(image, 0, 0);
const imageData = ctx.getImageData(0, 0, image.width, image.height);
diff --git a/fixtures/css/image-verification.xsml b/fixtures/css/image-verification.xsml
index 301eb08..3f6a1ed 100644
--- a/fixtures/css/image-verification.xsml
+++ b/fixtures/css/image-verification.xsml
@@ -13,27 +13,36 @@
-
+
![]()
+
。
From 0142d7cbf383514ce5eb6f0b9895cf288a1d03e4 Mon Sep 17 00:00:00 2001
From: neverbiasu <1751162157@qq.com>
Date: Thu, 4 Jul 2024 18:30:36 +0800
Subject: [PATCH 6/6] last dance
---
fixtures/css/image-verification.ts | 40 +++++++++++++++++++++++++---
fixtures/css/image-verification.xsml | 6 +++--
2 files changed, 40 insertions(+), 6 deletions(-)
diff --git a/fixtures/css/image-verification.ts b/fixtures/css/image-verification.ts
index dfa1fd6..9c670c2 100644
--- a/fixtures/css/image-verification.ts
+++ b/fixtures/css/image-verification.ts
@@ -15,8 +15,10 @@ spatialDocument.addEventListener('spaceReady', () => {
if (ctx == null) {
return;
}
- const leftImageData = getShapeFromImage(bitmap, 'rect');
- const rightImageData = cutShapeFromImage(bitmap, 'rect');
+
+ const shape = getRandomShape();
+ const leftImageData = getShapeFromImage(bitmap, shape);
+ const rightImageData = cutShapeFromImage(bitmap, shape);
const leftImageURL = await convertImageDataToDataURL(leftImageData);
const rightImageURL = await convertImageDataToDataURL(rightImageData);
@@ -49,12 +51,15 @@ async function convertImageDataToDataURL(imageData: ImageData): Promise
}
function moveImageWithRay() {
- div.addEventListener('mousemove', (event) => {
+ div.addEventListener('raymove', (event) => {
const mouseX = Math.round(event.x);
const gapX = mouseX - 128;
img1.style.transform = `translateX(${gapX}px)`;
if (gapX >= 763 && gapX <= 773) {
p.textContent = 'Success!';
+ p.style.color = 'white';
+ } else {
+ p.textContent = '';
}
})
}
@@ -63,11 +68,21 @@ function cutShapeFromImage(image, shape: string) {
const canvas = new OffscreenCanvas(image.width, image.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0);
-
ctx.globalCompositeOperation = 'destination-out';
if (shape === 'rect') {
ctx.fillRect(1/4 * image.width, 1/4 * image.height, 1/2 * image.width, 1/2 * image.height);
+ } else if (shape === 'circle') {
+ ctx.beginPath();
+ ctx.arc(image.width / 2, image.height / 2, Math.min(image.width, image.height) / 4, 0, 2 * Math.PI);
+ ctx.fill();
+ } else if (shape === 'triangle') {
+ ctx.beginPath();
+ ctx.moveTo(1/2 * image.width, 1/4 * image.height);
+ ctx.lineTo(1/4 * image.width, 3/4 * image.height);
+ ctx.lineTo(3/4 * image.width, 3/4 * image.height);
+ ctx.closePath();
+ ctx.fill();
}
const imageData = ctx.getImageData(0, 0, image.width, image.height);
@@ -83,6 +98,17 @@ function getShapeFromImage(image, shape: string) {
ctx.beginPath();
ctx.rect(1/4 * image.width, 1/4 * image.height, 1/2 * image.width, 1/2 * image.height);
ctx.clip();
+ } else if (shape === 'circle') {
+ ctx.beginPath();
+ ctx.arc(image.width / 2, image.height / 2, Math.min(image.width, image.height) / 4, 0, 2 * Math.PI);
+ ctx.clip();
+ } else if (shape === 'triangle') {
+ ctx.beginPath();
+ ctx.moveTo(1/2 * image.width, 1/4 * image.height);
+ ctx.lineTo(1/4 * image.width, 3/4 * image.height);
+ ctx.lineTo(3/4 * image.width, 3/4 * image.height);
+ ctx.closePath();
+ ctx.clip();
}
ctx.drawImage(image, 0, 0);
@@ -91,3 +117,9 @@ function getShapeFromImage(image, shape: string) {
return imageData;
}
+
+function getRandomShape() {
+ const shapes = ['rect', 'circle', 'triangle'];
+ const randomIndex = Math.floor(Math.random() * shapes.length);
+ return shapes[randomIndex];
+}
diff --git a/fixtures/css/image-verification.xsml b/fixtures/css/image-verification.xsml
index 3f6a1ed..e201bf5 100644
--- a/fixtures/css/image-verification.xsml
+++ b/fixtures/css/image-verification.xsml
@@ -17,9 +17,11 @@
![]()
-
。
+
.