Skip to content

Commit

Permalink
made js animations smoother
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Sep 2, 2023
1 parent c04d335 commit b4187e5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 additions & 3 deletions test2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,23 @@ var isRenderInProgress = false

var frameIndex = 0

proc doRender(time: float) =
var start: float = -1
var previousTimestep: float = 0

proc doRender(timestep: float) =
# echo "requestAnimationFrame ", time

if timestep == previousTimestep:
# echo "multiple per frame"
return

if start < 0 or builder.animatingNodes.len == 0:
start = timestep
builder.frameTime = 0
else:
builder.frameTime = timestep - previousTimestep
previousTimestep = timestep

defer:
if builder.animatingNodes.len > 0:
requestRender()
Expand Down Expand Up @@ -429,7 +443,6 @@ proc doRender(time: float) =
renderTime = renderTimer.elapsed.ms

frameTime = frameTimer.elapsed.ms
builder.frameTime = frameTime

# if frameTime > 20:
if logFrameTime:
Expand Down Expand Up @@ -466,7 +479,7 @@ proc drawNode(builder: UINodeBuilder, platform: BrowserPlatform, element: var El
css += "overflow: hidden;".cstring

if DrawBorder in node.flags:
css += "border: 1px solid ".cstring
css += "outline: 1px solid ".cstring
css += node.borderColor.myToHtmlHex
css += ";".cstring

Expand Down
2 changes: 1 addition & 1 deletion test_lib.nim
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ proc renderLine*(builder: UINodeBuilder, line: string, curs: Option[int], backgr
result = some (currentNode, "", rect(lastPartXW, 0, builder.charWidth, builder.textHeight))

# Fill rest of line with background
builder.panel(&{FillX, FillY, FillBackground}, backgroundColor = backgroundColor * 2)
builder.panel(&{FillX, FillY, FillBackground}, backgroundColor = backgroundColor)

proc renderText*(builder: UINodeBuilder, changed: bool, lines: openArray[string], first: int, cursor: (int, int), backgroundColor, textColor: Color, sizeToContentX = false, sizeToContentY = true, id = Id.none) =
var flags = &{MaskContent, OverlappingChildren}
Expand Down

0 comments on commit b4187e5

Please sign in to comment.