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 dacaa4d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
34 changes: 28 additions & 6 deletions docs/components/PaveRenderer.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<script lang="ts" setup>
import {throttledWatch, useCssVar, useElementSize} from '@vueuse/core'
import {computed, onMounted, ref, watch} from 'vue'
import {
throttledWatch,
useCssVar,
useElementSize,
useMouseInElement,
} from '@vueuse/core'
import {computed, onMounted, ref, watch, watchEffect} from 'vue'
import {createDrawFunction, setupEvalContextCreator} from './createDrawFunction'
import {vec2} from 'linearly'
const props = withDefaults(
defineProps<{
Expand All @@ -15,6 +21,22 @@ const props = withDefaults(
const canvas = ref<null | HTMLCanvasElement>(null)
const context = ref<null | CanvasRenderingContext2D>(null)
const {x: mouseX, y: mouseY} = useMouseInElement(canvas, {
target: canvas,
type: e => {
const el = e.target as HTMLElement
return [
((e.pageX - el.offsetLeft) / el.offsetWidth) * 100,
((e.pageY - el.offsetTop) / el.offsetHeight) * 100,
]
},
initialValue: {x: 50, y: 50},
})
const mouse = computed<vec2>(() => [mouseX.value, mouseY.value])
watchEffect(() => console.log(mouse.value))
const {width: canvasWidth, height: canvasHeight} = useElementSize(canvas)
onMounted(async () => {
Expand All @@ -30,7 +52,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 +72,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 dacaa4d

Please sign in to comment.