Skip to content

Commit

Permalink
Looking more like PHET
Browse files Browse the repository at this point in the history
  • Loading branch information
Patitotective committed May 26, 2024
1 parent 0f28bce commit d5167ab
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 272 deletions.
7 changes: 7 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
body {
margin: 0;
}

html, body, #ROOT {
height: 100%;
/* width: 100%;*/
}
Binary file added public/img/canon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 12 additions & 22 deletions src/frontend.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import karax/[karax, kajax, karaxdsl, vdom, vstyles, i18n, jdict, languages]

# import matter
import frontend/[routes, utils, patterns]
from frontend/simulations/parabola import nil
import frontend/simulations/parabola

type
State = ref object
prevTitle*: cstring # Title of the previous page when using navigateTo()
location*: Location
simulationIndex*: int # Current simulation index
matterLoaded*: bool
parabola*: ParabolaState

proc deepCopy(l: Location): Location =
Location(
Expand All @@ -28,7 +28,10 @@ proc deepCopy(l: Location): Location =
)

proc newState(): State =
State(location: window.location.deepCopy(), prevTitle: document.title, simulationIndex: -1)
result = State(location: window.location.deepCopy(), prevTitle: document.title,
parabola: initParabolaState()
)
result.parabola.addEventListeners()

proc navigateTo(uri: cstring) =
# Add uri to the history
Expand All @@ -42,38 +45,25 @@ var state = newState()
# addTranslation(esCo, "s/0/")

proc renderHome(params: Params): VNode =
navigateTo("/s/0")

buildHtml(tdiv):
text "Welcome to my grado project"

# proc renderSimulation(params: Params): VNode =
# buildHtml(tdiv):

const simulations = [
(load: parabola.load, render: parabola.render),
]

proc render(): VNode =
buildHtml(tdiv):
# renderHeader()
state.location.route([
r("/s/@id", proc(params: Params): VNode =
try:
state.simulationIndex = params["id"].parseInt()
except ValueError:
return renderError404()

if state.simulationIndex in simulations.low..simulations.high:
simulations[state.simulationIndex].render()
else:
renderError404()
r("/parabola", proc(params: Params): VNode =
state.parabola.render()
),
r("/", renderHome)
])

proc postRender() =
# Since matter needs to find the canvas element, if we load the simulation before karax has created the canvas element it won't work
if not state.matterLoaded and state.simulationIndex in simulations.low..simulations.high:
simulations[state.simulationIndex].load()
if not state.matterLoaded:
state.parabola.load()
state.matterLoaded = true

# This event is (usually only) called when the user moves back in history
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/simulations/matter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import std/[jsffi]

var
Matter* {.importjs, nodecl.}: JsObject
Body*, Vector*, Engine*, Render*, Runner*, Bodies*, Events*, Composite*, MouseConstraint*, Mouse*, Constraint*: JsObject
Body*, Vector*, Engine*, Render*, Runner*, Bodies*, Bounds*, Events*, Composite*, MouseConstraint*, Mouse*, Constraint*: JsObject

# This function is used to load Matter's aliases
# since when using plugins you should reload aliases
Expand All @@ -15,6 +15,7 @@ proc loadMatterAliases*() =
Render = Matter.Render
Runner = Matter.Runner
Bodies = Matter.Bodies
Bounds = Matter.Bounds
Events = Matter.Events
Composite = Matter.Composite
MouseConstraint = Matter.MouseConstraint
Expand Down
Loading

0 comments on commit d5167ab

Please sign in to comment.