feat: Create geogebra applet and implement some Joyce commands (#5)

Reviewed-on: #5
Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Co-committed-by: Glen Whitney <glen@studioinfinity.org>
This commit is contained in:
Glen Whitney 2023-08-30 03:56:13 +00:00 committed by Glen Whitney
parent 10146073c8
commit 97f7138d7b
2 changed files with 88 additions and 3 deletions

View File

@ -15,7 +15,7 @@
<table class="centered">
<tr><td align="center">
<applet code="Geometry" archive="Geometry.zip" width="410" height="370">
<param name="background" value="ffffff">
<param name="background" value="ffffdd">
<param name="title" value="An equilateral triangle inscribed in a rectangle">
<!-- the moving mechanism -->

View File

@ -1,2 +1,87 @@
import https://code.jquery.com/jquery-3.7.1.slim.js
$('body').append '<div id="helloDiv">Hello World</div>'
import https://code.jquery.com/jquery-3.7.1.js
joyceApplets := []
$('applet[code="Geometry"]').before (i, html) ->
id := `joyceApplet${i}`
joyceApplets.push { html, this.children, id,
width: parseInt(this.getAttribute 'width'),
height: parseInt(this.getAttribute 'height') }
`<div id="${id}"></div>`
jQuery.getScript 'https://www.geogebra.org/apps/deployggb.js', =>
for each jApp of joyceApplets
params := {
appName: 'classic',
jApp.width,
jApp.height,
appletOnLoad: (api) =>
for child of jApp.children
dispatchJcommand api, child
api.setCoordSystem(-10, 10 + jApp.width, -10, 10 + jApp.height)
}
geoApp := new GGBApplet params
geoApp.inject jApp.id
type Cmdr
command: string
callbacks: ((api) => boolean)[]
function dispatchJcommand(api, param)
val := param.getAttribute 'value'
switch param.getAttribute 'name'
'background'
api.setGraphicsOptions 1, bgColor: `#${val}`
'title'
api.evalCommand `TitlePoint = Corner(1,1)
Text("${val}", TitlePoint + (2,5))`
/e\[\d+\]/
{command, callbacks} := jToG val
if command
if api.evalCommand command
for each cb of callbacks
cb(api)
else
console.log `Geogebra command '${command}' translated from`,
val, 'failed.'
else
console.log `Unknown command '${val}'`
else
console.log `Unkown param ${param}`
function jToG(jCom: string): Cmdr
[name, klass, method, data, ...colors] := jCom.split(';')
if klass in classHandler
return classHandler[klass] name, method, data, colors
console.log `Unknown entity class ${klass}`
return ''
classHandler :=
point: (name: string, m: string, data: string, colors: string[]): Cmdr =>
command .= ''
callbacks .= []
switch m
/free|fixed/
command += `${name} = (${data})`
if m is 'fixed'
callbacks.push (api) => api.setFixed(name, true)
'perpendicular'
[center, direction] := data.split(',')
command += `${name} = Rotate(${direction}, 3*pi/2, ${center})`
return {command, callbacks}
line: (name: string, m: string, data: string, colors: string[]): Cmdr =>
command .= ''
callbacks .= []
switch m
'connect'
command += `${name} = Segment(${data})`
return {command, callbacks}
circle: (name: string, m: string, data: string, colors: string[]): Cmdr =>
command .= ''
callbacks .= []
switch m
'radius'
[center, point] := data.split(',')
command += `${name} = Circle(${center}, ${point})`
return {command, callbacks}