support rotation, refactor VideoScroll.vue

master
Tomáš Mládek 2021-01-11 22:17:29 +01:00
parent a78318dd07
commit 32de482742
3 changed files with 54 additions and 24 deletions

View File

@ -27,6 +27,7 @@ import createPanZoom, {PanZoom} from "panzoom";
import VideoScroll, {VideoScrollDef, VideoScrollDirection} from "@/components/VideoScroll.vue"; import VideoScroll, {VideoScrollDef, VideoScrollDirection} from "@/components/VideoScroll.vue";
import AudioArea, {AudioAreaDef} from "@/components/AudioArea.vue"; import AudioArea, {AudioAreaDef} from "@/components/AudioArea.vue";
import Stats from "stats.js"; import Stats from "stats.js";
import {rotate} from "@/utils";
export default defineComponent({ export default defineComponent({
name: "SVGContent", name: "SVGContent",
@ -264,11 +265,27 @@ async function processScrolls(svg: XMLDocument): Promise<VideoScrollDef[]> {
const preURL = fileFetch.url.replace(/\/files.lst$/, ""); const preURL = fileFetch.url.replace(/\/files.lst$/, "");
const files = (await fileFetch.text()).split("\n").filter(Boolean).map((str) => `${preURL}/${str}`); const files = (await fileFetch.text()).split("\n").filter(Boolean).map((str) => `${preURL}/${str}`);
let x = el.x.baseVal.value;
let y = el.y.baseVal.value;
let w = el.width.baseVal.value;
let h = el.height.baseVal.value;
let angle = 0;
const transform = el.attributes.getNamedItem("transform");
const rotateResult = /rotate\((-?[0-9.]+)\)/.exec(transform?.value || "");
if (rotateResult) {
angle = parseFloat(rotateResult[1]);
const [ncx, ncy] = rotate(x + w / 2, y + h / 2, 0, 0, angle);
x = ncx - w / 2;
y = ncy - h / 2;
}
return { return {
top: el.y.baseVal.value * ratio, top: y * ratio,
left: el.x.baseVal.value * ratio, left: x * ratio,
width: el.width.baseVal.value * ratio, angle,
height: el.height.baseVal.value * ratio, width: w * ratio,
height: h * ratio,
direction: directionString as VideoScrollDirection, direction: directionString as VideoScrollDirection,
files files
}; };

View File

@ -5,22 +5,22 @@
:style="{ :style="{
top: `${Math.round(definition.top)}px`, top: `${Math.round(definition.top)}px`,
left: `${Math.round(definition.left)}px`, left: `${Math.round(definition.left)}px`,
width: isHorizontal(definition) ? `${Math.round(definition.width)}px` : 'auto', width: isHorizontal ? `${Math.round(definition.width)}px` : 'auto',
height: isHorizontal(definition) ? 'auto' : `${Math.round(definition.height)}px`, height: isHorizontal ? 'auto' : `${Math.round(definition.height)}px`,
rotate: `${definition.angle}deg`
}" }"
/> />
<!--suppress RequiredAttributes --> <!--suppress RequiredAttributes -->
<img v-for="(file, idx) in definition.files.slice(1)" <img v-for="file in dynamicFiles"
:data-src="file" :data-src="file.src"
:data-direction="definition.direction" :data-direction="definition.direction"
:style="{ :style="{
top: `${Math.round(definition.top) + top: `${Math.round(file.top)}px`,
(isHorizontal(definition) ? 0 : (definition.height * (idx + 1) * directionSign))}px`, left: `${Math.round(file.left)}px`,
left: `${Math.round(definition.left) +
(isHorizontal(definition) ? (definition.width * (idx + 1) * directionSign) : 0)}px`,
width: `${Math.round(definition.width)}px`, width: `${Math.round(definition.width)}px`,
height: `${Math.round(definition.height)}px`, height: `${Math.round(definition.height)}px`,
rotate: `${definition.angle}deg`
}" }"
/> />
</div> </div>
@ -28,6 +28,7 @@
<script lang="ts"> <script lang="ts">
import {defineComponent, onMounted, ref} from "vue"; import {defineComponent, onMounted, ref} from "vue";
import {rotate} from "@/utils";
export default defineComponent({ export default defineComponent({
name: "VideoScroll", name: "VideoScroll",
@ -38,18 +39,21 @@ export default defineComponent({
} }
}, },
computed: { computed: {
directionSign() { dynamicFiles(): { [key: string]: string } {
if (this.definition.direction == VideoScrollDirection.RIGHT || return this.definition.files.slice(1).map((src: string, idx: number) => {
this.definition.direction == VideoScrollDirection.DOWN) { const cy = this.definition.top +
return 1; (this.isHorizontal ? 0 : (this.definition.height * (idx + 1) * this.direction));
} else { const cx = this.definition.left +
return -1; (this.isHorizontal ? (this.definition.width * (idx + 1) * this.direction) : 0);
} const [left, top] = rotate(cx, cy, this.definition.left, this.definition.top, this.definition.angle);
} return {top, left, src};
}, });
methods: { },
isHorizontal(definition: VideoScrollDef): boolean { isHorizontal(): boolean {
return definition.direction === "left" || definition.direction === "right"; return this.definition.direction === "left" || this.definition.direction === "right";
},
direction(): number {
return (this.definition.direction === "right" || this.definition.direction === "down") ? 1 : -1;
} }
}, },
setup(props) { setup(props) {
@ -95,6 +99,7 @@ export enum VideoScrollDirection {
export interface VideoScrollDef { export interface VideoScrollDef {
top: number, top: number,
left: number, left: number,
angle: number,
width: number, width: number,
height: number, height: number,
direction: VideoScrollDirection, direction: VideoScrollDirection,

8
app/src/utils.ts Normal file
View File

@ -0,0 +1,8 @@
export function rotate(x: number, y: number, cx: number, cy: number, angleDegrees: number): [number, number] {
const angleRad = (Math.PI / 180) * angleDegrees * -1;
const cos = Math.cos(angleRad);
const sin = Math.sin(angleRad);
const nx = (cos * (x - cx)) + (sin * (y - cy)) + cx;
const ny = (cos * (y - cy)) - (sin * (x - cx)) + cy;
return [nx, ny];
}