Skip to content

Commit

Permalink
Add mouse parameter to the sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Jul 29, 2024
1 parent 4cfe96e commit 4c29c16
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
50 changes: 44 additions & 6 deletions docs/components/PaveRenderer.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
<script lang="ts" setup>
import {throttledWatch, useCssVar, useElementSize} from '@vueuse/core'
import {
throttledWatch,
useCssVar,
useElementBounding,
useElementSize,
useMouseInElement,
watchOnce,
} from '@vueuse/core'
import {computed, onMounted, ref, watch} from 'vue'
import {createDrawFunction, setupEvalContextCreator} from './createDrawFunction'
import {vec2} from 'linearly'
const props = withDefaults(
defineProps<{
Expand All @@ -15,7 +23,37 @@ const props = withDefaults(
const canvas = ref<null | HTMLCanvasElement>(null)
const context = ref<null | CanvasRenderingContext2D>(null)
const {width: canvasWidth, height: canvasHeight} = useElementSize(canvas)
const {
x: mouseX,
y: mouseY,
isOutside,
} = useMouseInElement(canvas, {
target: canvas,
})
const onceHovered = ref(false)
watchOnce(isOutside, () => {
if (!isOutside.value) {
onceHovered.value = true
}
})
const mouse = computed<vec2>(() =>
onceHovered.value
? [
((mouseX.value - canvasX.value) / canvasWidth.value) * 100,
((mouseY.value - canvasY.value) / canvasHeight.value) * 100,
]
: [50, 50]
)
const {
width: canvasWidth,
height: canvasHeight,
x: canvasX,
y: canvasY,
} = useElementBounding(canvas)
onMounted(async () => {
context.value = canvas.value?.getContext('2d') ?? null
Expand All @@ -30,7 +68,7 @@ onMounted(async () => {
return createDrawContext(ctx)
})
const evalFn = ref<((time: number) => void) | null>(null)
const evalFn = ref<((arg: {time: number; mouse: vec2}) => void) | null>(null)
throttledWatch(
() =>
Expand All @@ -50,12 +88,12 @@ onMounted(async () => {
)
watch(
() => [props.time, evalFn.value] as const,
([time, evalFn]) => {
() => [props.time, mouse.value, evalFn.value] as const,
([time, mouse, evalFn]) => {
if (!evalFn) return
try {
evalFn(time)
evalFn({time, mouse})
} catch (e) {
// eslint-disable-next-line no-console
console.error(e)
Expand Down
8 changes: 4 additions & 4 deletions docs/components/createDrawFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ export function createDrawFunction(

try {
const draw = saferEval(
`(time) => {\n${code}\n}`,
`({time, mouse}) => {\n${code}\n}`,
evalContext
) as unknown as (time: number) => void
) as unknown as (arg: {time: number; mouse: vec2}) => void

return (time: number) => {
return (arg: {time: number; mouse: vec2}) => {
canvasContext.clearRect(0, 0, canvas.width, canvas.height)
canvasContext.resetTransform()
canvasContext.transform(...mat2d.fromScaling([scale, scale]))
draw(time)
draw(arg)
}
} catch (e) {
// eslint-disable-next-line no-console
Expand Down
2 changes: 1 addition & 1 deletion docs/components/exportVideo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export async function exportVideo(code: string, onlyCanvas = false) {
for (let i = 0; i < frameCount; i++) {
const time = i / frameCount

evalFn(time)
evalFn({time, mouse: [50, 50]})

if (!onlyCanvas) {
videoContext.clearRect(0, 0, 1920, 1080)
Expand Down
3 changes: 3 additions & 0 deletions docs/sandbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ context: CanvasRenderingContext2D
// Current time ranging from 0 to 1
time: number

// The position of the mouse ranging from 0 to 100. Initial value is [50, 50]
mouse: [x: number, y: number]

// Shorhands for drawing functions
stroke: (path: Path, color = accentCoor, width = 1) => void
fill: (path: Path, color = accentColor) => void
Expand Down

0 comments on commit 4c29c16

Please sign in to comment.