-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f5fa5b
commit 9f5e7ab
Showing
5 changed files
with
146 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<template> | ||
<div class="zoom-control"> | ||
<button @click="zoomIn"> | ||
+ | ||
</button> | ||
<button @click="zoomOut"> | ||
- | ||
</button> | ||
<button @click="resetZoom"> | ||
Reset | ||
</button> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { defineComponent, inject } from 'vue' | ||
const zoomPlugin = inject('zoomPlugin') | ||
const zoomIn = () => { | ||
zoomPlugin?.wheel({ deltaY: -1 } as WheelEvent) | ||
} | ||
const zoomOut = () => { | ||
zoomPlugin?.wheel({ deltaY: 1 } as WheelEvent) | ||
} | ||
const resetZoom = () => { | ||
zoomPlugin?.wheel({ deltaY: 0 } as WheelEvent) // Customize this based on actual reset logic | ||
} | ||
</script> | ||
|
||
<style scoped> | ||
.zoom-control { | ||
position: absolute; | ||
top: 20px; | ||
right: 20px; | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
.zoom-control button { | ||
margin: 5px 0; | ||
padding: 10px; | ||
border: none; | ||
background-color: var(--green); | ||
color: white; | ||
font-size: 16px; | ||
cursor: pointer; | ||
} | ||
.zoom-control button:hover { | ||
background-color: var(--dark-green); | ||
} | ||
</style> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { AreaPlugin, Zoom } from "rete-area-plugin" | ||
import anime from "animejs/lib/anime.es.js" | ||
|
||
function screenToArea(x: number, y: number, t: any) { | ||
const { x: tx, y: ty, k } = t | ||
|
||
return { x: (x - tx) / k, y: (y - ty) / k } | ||
} | ||
|
||
function areaToScreen(x: number, y: number, t: any) { | ||
const { x: tx, y: ty, k } = t | ||
|
||
return { x: x * k + tx, y: y * k + ty } | ||
} | ||
|
||
export class SmoothZoom extends Zoom { | ||
animation?: any | ||
|
||
constructor( | ||
intensity: number, | ||
private duration: number, | ||
private easing: string, | ||
private area: AreaPlugin<any> | ||
) { | ||
super(intensity) | ||
} | ||
|
||
wheel = (e: WheelEvent) => { | ||
e.preventDefault() | ||
|
||
const isNegative = e.deltaY < 0 | ||
const delta = isNegative ? this.intensity : -this.intensity | ||
const { left, top } = this.container.getBoundingClientRect() | ||
const ox = e.clientX - left | ||
const oy = e.clientY - top | ||
|
||
const coords = screenToArea(ox, oy, this.area.area.transform) | ||
|
||
const { k } = this.area.area.transform | ||
const targets = { | ||
zoom: k | ||
} | ||
const { duration, easing } = this | ||
|
||
if (this.animation) { | ||
this.animation.reset() | ||
} | ||
this.animation = anime({ | ||
targets, | ||
x: coords.x, | ||
y: coords.y, | ||
zoom: k * (1 + delta), | ||
duration, | ||
easing, | ||
update: () => { | ||
const currentTransform = this.area.area.transform | ||
|
||
const coordinates = areaToScreen(coords.x, coords.y, currentTransform) | ||
|
||
const nextX = coordinates.x - coords.x * targets.zoom | ||
const nextY = coordinates.y - coords.y * targets.zoom | ||
|
||
this.area.area.zoom( | ||
targets.zoom, | ||
nextX - currentTransform.x, | ||
nextY - currentTransform.y | ||
) | ||
} | ||
}) | ||
} | ||
|
||
destroy() { | ||
super.destroy() | ||
if (this.animation) { | ||
this.animation.reset() | ||
} | ||
} | ||
} |