Skip to content

Commit 817cc8f

Browse files
committed
refactor: Improvement implements for syncCtrlElPos
1 parent 5345850 commit 817cc8f

File tree

3 files changed

+58
-39
lines changed

3 files changed

+58
-39
lines changed

.changeset/lazy-bears-decide.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@webav/av-canvas': feat
3+
'@webav/av-cliper': feat
4+
'@webav/av-recorder': feat
5+
'@webav/internal-utils': feat
6+
---
7+
8+
feat: support interactable for VisibleSprite #456

packages/av-canvas/demo/video-editor.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ function App() {
300300
new ImgClip((await fetch('./img/bunny.png')).body!),
301301
);
302302
await avCvs?.addSprite(spr);
303+
// spr.interactable = 'interactive';
303304
addSprite2Track('3-img', spr, '图片');
304305
}}
305306
>
@@ -499,6 +500,7 @@ async function createFileWriter(
499500
}
500501

501502
async function loadFile(
503+
// @ts-ignore
502504
accept: Record<MIMEType, FileExtension | FileExtension[]>,
503505
) {
504506
const [fileHandle] = await window.showOpenFilePicker({

packages/av-canvas/src/sprites/render-ctrl.ts

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -107,59 +107,68 @@ function syncCtrlElPos(
107107
rectEl: HTMLElement,
108108
ctrlsEl: Record<TCtrlKey, HTMLElement>,
109109
): void {
110+
if (s.interactable === 'disabled') {
111+
rectEl.style.display = 'none';
112+
return;
113+
}
114+
rectEl.style.display = '';
115+
110116
const cvsRatio = getCvsRatio(cvsEl);
111117
const { x, y, w, h, angle } = s.rect;
118+
112119
Object.assign(rectEl.style, {
113120
left: `${x * cvsRatio.w}px`,
114121
top: `${y * cvsRatio.h}px`,
115122
width: `${w * cvsRatio.w}px`,
116123
height: `${h * cvsRatio.h}px`,
117-
rotate: `${angle}rad`,
124+
transform: `rotate(${angle}rad)`,
118125
});
119-
Object.entries(getRectCtrls(cvsEl, s.rect)).forEach(([k, { x, y, w, h }]) => {
120-
// ctrl 是相对中心点定位的
121-
const baseStyle = {
126+
127+
const ctrlPosMap = getRectCtrls(cvsEl, s.rect);
128+
129+
for (const k in ctrlsEl) {
130+
const key = k as TCtrlKey;
131+
const el = ctrlsEl[key];
132+
const pos = ctrlPosMap[key];
133+
134+
if (pos == null) {
135+
el.style.display = 'none';
136+
continue;
137+
}
138+
139+
const baseStyle: Record<string, string> = {
140+
width: `${pos.w * cvsRatio.w}px`,
141+
height: `${pos.h * cvsRatio.h}px`,
142+
transform: `translate(${pos.x * cvsRatio.w}px, ${pos.y * cvsRatio.h}px)`,
122143
left: '50%',
123144
top: '50%',
124-
width: `${w * cvsRatio.w}px`,
125-
height: `${h * cvsRatio.h}px`,
126-
transform: `translate(${x * cvsRatio.w}px, ${y * cvsRatio.h}px)`,
127145
};
128-
ctrlsEl[k as TCtrlKey].innerHTML = '';
129-
if (k === 'rotate') {
130-
Object.assign(ctrlsEl[k as TCtrlKey].style, {
131-
display: s.interactable === 'interactive' ? 'block' : 'none',
132-
...baseStyle,
133-
});
134-
} else {
135-
if (s.interactable === 'interactive') {
136-
Object.assign(ctrlsEl[k as TCtrlKey].style, {
146+
147+
let customStyle: Record<string, string> = { display: 'none' };
148+
el.innerHTML = '';
149+
150+
switch (s.interactable) {
151+
case 'interactive':
152+
customStyle = {
137153
display: 'block',
138154
backgroundColor: '#fff',
139155
border: '1px solid #3ee',
140-
...baseStyle,
141-
});
142-
} else if (s.interactable === 'selectable') {
143-
Object.assign(ctrlsEl[k as TCtrlKey].style, {
144-
display: 'flex',
145-
justifyContent: 'center',
146-
alignItems: 'center',
147-
backgroundColor: 'transparent',
148-
border: 'none',
149-
...baseStyle,
150-
});
151-
ctrlsEl[k as TCtrlKey].innerHTML = CloseSvg;
152-
} else {
153-
Object.assign(ctrlsEl[k as TCtrlKey].style, {
154-
display: 'none',
155-
...baseStyle,
156-
});
157-
}
156+
};
157+
break;
158+
case 'selectable':
159+
if (key !== 'rotate') {
160+
customStyle = {
161+
display: 'flex',
162+
justifyContent: 'center',
163+
alignItems: 'center',
164+
backgroundColor: 'transparent',
165+
border: 'none',
166+
};
167+
el.innerHTML = CloseSvg;
168+
}
169+
break;
158170
}
159-
});
160-
if (s.interactable === 'disabled') {
161-
rectEl.style.display = 'none';
162-
} else {
163-
rectEl.style.display = '';
171+
172+
Object.assign(el.style, baseStyle, customStyle);
164173
}
165174
}

0 commit comments

Comments
 (0)