Skip to content

Commit 1992e61

Browse files
authored
Merge pull request #512 from vizzuhq/rendering-fix
Rendering fixed, update separated to update and render on wasm api
2 parents 6119300 + 9a1c453 commit 1992e61

File tree

12 files changed

+293
-63
lines changed

12 files changed

+293
-63
lines changed

project/cmake/weblib/emcc.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} \
2020
'_vizzu_pointerLeave',\
2121
'_vizzu_wheel',\
2222
'_vizzu_update',\
23+
'_vizzu_render',\
2324
'_vizzu_setLogging',\
2425
'_vizzu_errorMessage',\
2526
'_vizzu_version',\

src/apps/weblib/cinterface.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,20 @@ void vizzu_wheel(APIHandles::Chart chart,
180180
return Interface::getInstance().wheel(chart, canvas, delta);
181181
}
182182

183-
void vizzu_update(APIHandles::Chart chart,
183+
void vizzu_update(APIHandles::Chart chart, double timeInMSecs)
184+
{
185+
return Interface::getInstance().update(chart, timeInMSecs);
186+
}
187+
188+
void vizzu_render(APIHandles::Chart chart,
184189
APIHandles::Canvas canvas,
185190
double width,
186-
double height,
187-
double timeInMSecs,
188-
bool render)
191+
double height)
189192
{
190-
return Interface::getInstance()
191-
.update(chart, canvas, width, height, timeInMSecs, render);
193+
return Interface::getInstance().render(chart,
194+
canvas,
195+
width,
196+
height);
192197
}
193198

194199
const char *style_getList() { return Interface::getStyleList(); }

src/apps/weblib/cinterface.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,11 @@ extern void vizzu_wheel(APIHandles::Chart chart,
6262
APIHandles::Canvas canvas,
6363
double delta);
6464
extern void vizzu_setLogging(bool enable);
65-
extern void vizzu_update(APIHandles::Chart chart,
65+
extern void vizzu_update(APIHandles::Chart chart, double timeInMSecs);
66+
extern void vizzu_render(APIHandles::Chart chart,
6667
APIHandles::Canvas canvas,
6768
double width,
68-
double height,
69-
double timeInMSecs,
70-
bool render);
69+
double height);
7170
extern const char *vizzu_errorMessage(
7271
APIHandles::Exception exceptionPtr,
7372
const std::type_info *typeinfo);

src/apps/weblib/interface.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ ObjectRegistry::Handle Interface::createChart()
329329
{
330330
auto &&widget = std::make_shared<UI::ChartWidget>();
331331

332-
auto handle = objects.reg(std::move(widget));
332+
auto handle = objects.reg(widget);
333333

334334
widget->openUrl = [handle](const std::string &url)
335335
{
@@ -356,11 +356,7 @@ void Interface::setLogging(bool enable)
356356
}
357357

358358
void Interface::update(ObjectRegistry::Handle chart,
359-
ObjectRegistry::Handle canvas,
360-
double width,
361-
double height,
362-
double timeInMSecs,
363-
bool render)
359+
double timeInMSecs)
364360
{
365361
auto &&widget = objects.get<UI::ChartWidget>(chart);
366362

@@ -373,15 +369,23 @@ void Interface::update(ObjectRegistry::Handle chart,
373369
::Anim::TimePoint time(nanoSecs);
374370

375371
widget->getChart().getAnimControl().update(time);
372+
}
376373

377-
if (render) {
378-
const Geom::Size size{width, height};
379-
auto ptr = objects.get<Vizzu::Main::JScriptCanvas>(canvas);
380-
ptr->frameBegin();
381-
widget->onUpdateSize(size);
382-
widget->onDraw(ptr);
383-
ptr->frameEnd();
384-
}
374+
void Interface::render(ObjectRegistry::Handle chart,
375+
ObjectRegistry::Handle canvas,
376+
double width,
377+
double height)
378+
{
379+
auto &&widget = objects.get<UI::ChartWidget>(chart);
380+
auto &&ptr = objects.get<Vizzu::Main::JScriptCanvas>(canvas);
381+
382+
ptr->frameBegin();
383+
384+
widget->onUpdateSize({width, height});
385+
386+
widget->onDraw(ptr);
387+
388+
ptr->frameEnd();
385389
}
386390

387391
void Interface::pointerDown(ObjectRegistry::Handle chart,

src/apps/weblib/interface.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,11 @@ class Interface
4141
void wheel(ObjectRegistry::Handle chart,
4242
ObjectRegistry::Handle canvas,
4343
double delta);
44-
void update(ObjectRegistry::Handle chart,
44+
void update(ObjectRegistry::Handle chart, double timeInMSecs);
45+
void render(ObjectRegistry::Handle chart,
4546
ObjectRegistry::Handle canvas,
4647
double width,
47-
double height,
48-
double timeInMSecs,
49-
bool render);
48+
double height);
5049

5150
ObjectRegistry::Handle storeAnim(ObjectRegistry::Handle chart);
5251
void restoreAnim(ObjectRegistry::Handle chart,

src/apps/weblib/ts-api/chart.ts

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ import { Events, EventType, EventHandler, EventMap } from './events.js'
1212
import { Mirrored } from './tsutils.js'
1313
import { VizzuOptions } from './vizzu.js'
1414
import { AnimControl } from './animcontrol.js'
15-
import { PluginRegistry, Hooks, RenderContext } from './plugins.js'
15+
import {
16+
PluginRegistry,
17+
Hooks,
18+
RenderContext,
19+
UpdateContext,
20+
RenderControlMode
21+
} from './plugins.js'
1622
import { Logging } from './plugins/logging.js'
1723
import { Shorthands } from './plugins/shorthands.js'
1824
import { PivotData } from './plugins/pivotdata.js'
@@ -36,7 +42,7 @@ export class Chart implements ChartInterface {
3642
private _data: Data
3743
private _events: Events
3844
private _plugins: PluginRegistry
39-
private _needsUpdate = true
45+
private _changed = true
4046

4147
constructor(module: Module, options: VizzuOptions, plugins: PluginRegistry) {
4248
this._options = options
@@ -75,35 +81,56 @@ export class Chart implements ChartInterface {
7581

7682
start(): void {
7783
const ctx = {
78-
update: (force: boolean): void => this.updateFrame(force)
84+
update: (force: boolean): void => this.updateAndRender(force)
7985
}
8086
this._plugins.hook(Hooks.start, ctx).default(() => {
81-
this.updateFrame()
87+
this.updateAndRender()
8288
})
8389
}
8490

85-
updateFrame(force: boolean = false): void {
91+
private updateAndRender(force: boolean = false): void {
92+
this._update()
93+
this._render(force)
94+
}
95+
96+
private _update(): void {
97+
const ctx: UpdateContext = {
98+
timeInMSecs: null
99+
}
100+
this._plugins.hook(Hooks.update, ctx).default((ctx) => {
101+
if (ctx.timeInMSecs) {
102+
this._cChart.update(ctx.timeInMSecs)
103+
}
104+
})
105+
}
106+
107+
private _render(force: boolean): void {
108+
const control = force ? RenderControlMode.forced : RenderControlMode.disabled
86109
const ctx: RenderContext = {
87110
renderer: null,
88-
timeInMSecs: null,
89-
enable: true,
90-
force,
111+
control: control,
112+
changed: this._changed,
91113
size: { x: 0, y: 0 }
92114
}
93115
this._plugins.hook(Hooks.render, ctx).default((ctx) => {
94-
if (ctx.size.x >= 1 && ctx.size.y >= 1 && ctx.timeInMSecs !== null && ctx.renderer) {
95-
const render = ctx.force || (ctx.enable && this._needsUpdate)
96-
ctx.renderer.canvas = this._cCanvas
97-
this._module.registerRenderer(this._cCanvas, ctx.renderer)
98-
this._cChart.update(this._cCanvas, ctx.size.x, ctx.size.y, ctx.timeInMSecs, render)
99-
this._module.unregisterRenderer(this._cCanvas)
100-
this._needsUpdate = false
101-
}
116+
if (ctx.size.x < 1 || ctx.size.y < 1 || !ctx.renderer) return
117+
118+
const shouldRender =
119+
ctx.control === RenderControlMode.forced ||
120+
(ctx.control === RenderControlMode.allowed && ctx.changed)
121+
122+
if (!shouldRender) return
123+
124+
ctx.renderer.canvas = this._cCanvas
125+
this._module.registerRenderer(this._cCanvas, ctx.renderer)
126+
this._cChart.render(this._cCanvas, ctx.size.x, ctx.size.y)
127+
this._module.unregisterRenderer(this._cCanvas)
128+
this._changed = false
102129
})
103130
}
104131

105132
doChange(): void {
106-
this._needsUpdate = true
133+
this._changed = true
107134
}
108135

109136
openUrl(url: number): void {

src/apps/weblib/ts-api/cvizzu.types.d.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,9 @@ export interface CVizzu {
9191
_vizzu_pointerLeave(chart: CChartPtr, canvas: CCanvasPtr, pointerId: number): void
9292
_vizzu_wheel(chart: CChartPtr, canvas: CCanvasPtr, delta: number): void
9393
_vizzu_setLogging(enable: boolean): void
94-
_vizzu_update(
95-
chart: CChartPtr,
96-
canvas: CCanvasPtr,
97-
width: number,
98-
height: number,
99-
time: number,
100-
render: boolean
101-
): void
94+
_vizzu_update(chart: CChartPtr, time: number): void
95+
_vizzu_render(chart: CChartPtr, canvas: CCanvasPtr, width: number, height: number): void
96+
10297
_vizzu_errorMessage(exceptionPtr: CException, typeinfo: CTypeInfo): CString
10398
_vizzu_version(): CString
10499
_data_addDimension(

src/apps/weblib/ts-api/module/cchart.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ export class CChart extends CObject {
4242
this.animOptions = this._makeAnimOptions()
4343
}
4444

45-
update(cCanvas: CCanvas, width: number, height: number, time: number, render: boolean): void {
45+
update(time: number): void {
46+
this._call(this._wasm._vizzu_update)(time)
47+
}
48+
49+
render(cCanvas: CCanvas, width: number, height: number): void {
4650
this._cCanvas = cCanvas
47-
this._call(this._wasm._vizzu_update)(cCanvas.getId(), width, height, time, render)
51+
this._call(this._wasm._vizzu_render)(cCanvas.getId(), width, height)
4852
}
4953

5054
animate(callback: (ok: boolean) => void): void {

src/apps/weblib/ts-api/plugins.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { Canvas } from './module/canvas.js'
1010
export enum Hooks {
1111
/** Called once on startup for start the rendering loop. */
1212
start = 'start',
13+
/** Called when updating the chart due to time change. */
14+
update = 'update',
1315
/** Called on rendering. */
1416
render = 'render',
1517
/** Called when the animate() parameters gets set in the library to prepare
@@ -38,11 +40,20 @@ export interface StartContext {
3840
update: (force: boolean) => void
3941
}
4042

43+
export interface UpdateContext {
44+
timeInMSecs: number | null
45+
}
46+
47+
export enum RenderControlMode {
48+
forced = 'forced',
49+
allowed = 'allowed',
50+
disabled = 'disabled'
51+
}
52+
4153
export interface RenderContext {
4254
renderer: (CRenderer & Canvas) | null
43-
timeInMSecs: number | null
44-
enable: boolean
45-
force: boolean
55+
control: RenderControlMode
56+
changed: boolean
4657
size: Point
4758
}
4859

@@ -63,6 +74,7 @@ export interface RunAnimationContext {
6374

6475
export interface HookContexts {
6576
[Hooks.start]: StartContext
77+
[Hooks.update]: UpdateContext
6678
[Hooks.render]: RenderContext
6779
[Hooks.prepareAnimation]: PrepareAnimationContext
6880
[Hooks.registerAnimation]: RegisterAnimationContext

src/apps/weblib/ts-api/plugins/clock.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Plugin, PluginApi, PluginHooks, RenderContext } from '../plugins.js'
1+
import { Plugin, PluginApi, PluginHooks, UpdateContext } from '../plugins.js'
22

33
export interface ClockApi extends PluginApi {
44
/** Returns the actual time in miliseconds. */
@@ -16,7 +16,7 @@ export class Clock implements Plugin {
1616

1717
get hooks(): PluginHooks {
1818
const hooks = {
19-
render: (ctx: RenderContext, next: () => void): void => {
19+
update: (ctx: UpdateContext, next: () => void): void => {
2020
if (ctx.timeInMSecs === null) ctx.timeInMSecs = this._now()
2121
next()
2222
}

src/apps/weblib/ts-api/plugins/rendercontrol.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { Plugin, PluginApi, PluginHooks, RenderContext, StartContext } from '../plugins.js'
1+
import {
2+
Plugin,
3+
PluginApi,
4+
PluginHooks,
5+
RenderContext,
6+
UpdateContext,
7+
StartContext,
8+
RenderControlMode
9+
} from '../plugins.js'
210

311
export interface RenderControlApi extends PluginApi {
412
/** Re-renders the chart. */
@@ -27,12 +35,17 @@ export class RenderControl implements Plugin {
2735
this._update = ctx.update
2836
next()
2937
},
30-
render: (ctx: RenderContext, next: () => void): void => {
38+
update: (ctx: UpdateContext, next: () => void): void => {
3139
if (this._timeInMSecs !== null) {
3240
ctx.timeInMSecs = this._timeInMSecs
3341
this._timeInMSecs = null
3442
}
35-
ctx.enable = this._enabled
43+
next()
44+
},
45+
render: (ctx: RenderContext, next: () => void): void => {
46+
if (ctx.control === RenderControlMode.disabled && this._enabled) {
47+
ctx.control = RenderControlMode.allowed
48+
}
3649
next()
3750
}
3851
}

0 commit comments

Comments
 (0)