Skip to content

Commit

Permalink
chore: remove unsed parentSize on renderer & fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
romgere committed May 30, 2024
1 parent cd84ec8 commit 882e873
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 53 deletions.
14 changes: 5 additions & 9 deletions app/components/three-preview.hbs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
{{yield (hash
renderer=(component 'three-preview/renderer'
mesh=@mesh
parentSize=@parentSize
nearCamera=@nearCamera
{{yield
(hash
renderer=(component 'three-preview/renderer' mesh=@mesh nearCamera=@nearCamera)
size=(component 'three-preview/size' mesh=@mesh)
)
size=(component 'three-preview/size'
mesh=@mesh
)
)}}
}}
5 changes: 1 addition & 4 deletions app/components/three-preview/renderer.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
<canvas
...attributes
{{three-renderer @mesh parentSize=@parentSize nearCamera=@nearCamera}}
>
<canvas ...attributes {{three-renderer @mesh nearCamera=@nearCamera}}>
</canvas>
20 changes: 5 additions & 15 deletions app/modifiers/three-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const {
} = config;

type namedArgs = {
parentSize?: boolean;
nearCamera?: boolean;
};

Expand Down Expand Up @@ -79,11 +78,8 @@ export default class ThreeRendererModifier extends Modifier<ThreeRendererModifie
preserveDrawingBuffer: true, // use this to allow creation of image from canvas
});
this.renderer.setPixelRatio(window.devicePixelRatio);
this.renderer.setSize(
// for parent sizing set size to 1x1 & let the renderFrame adapt the size accordingly later...
this.namedArgs.parentSize ? 1 : 1024,
this.namedArgs.parentSize ? 1 : 768,
);
// set size to 1x1 & let the renderFrame adapt the size accordingly later...
this.renderer.setSize(1, 1);
this.renderer.setClearColor(0xffffff, 1);
}

Expand Down Expand Up @@ -182,9 +178,7 @@ export default class ThreeRendererModifier extends Modifier<ThreeRendererModifie

this.animationFrameRequestID = requestAnimationFrame(() => this.renderFrame());

const { offsetWidth: width, offsetHeight: height } = this.namedArgs.parentSize
? this.canvas?.parentElement ?? this.canvas
: this.canvas;
const { offsetWidth: width, offsetHeight: height } = this.canvas?.parentElement ?? this.canvas;

if (this.rendererSize.width !== width || this.rendererSize.height !== height) {
this.renderer.setSize(width, height);
Expand All @@ -201,12 +195,8 @@ export default class ThreeRendererModifier extends Modifier<ThreeRendererModifie
this.renderer.render(this.scene, this.camera);
}

modify(
element: HTMLCanvasElement,
[mesh]: [THREE.Mesh | undefined],
{ parentSize, nearCamera }: namedArgs,
) {
this.namedArgs = { parentSize, nearCamera };
modify(element: HTMLCanvasElement, [mesh]: [THREE.Mesh | undefined], { nearCamera }: namedArgs) {
this.namedArgs = { nearCamera };

if (!this.canvas) {
this.canvas = element;
Expand Down
2 changes: 1 addition & 1 deletion app/templates/app/generator.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
</calcite-shell-panel>

<calcite-panel>
<ThreePreview @mesh={{this.mesh.value}} @parentSize={{true}} @nearCamera={{true}} as |preview|>
<ThreePreview @mesh={{this.mesh.value}} @nearCamera={{true}} as |preview|>

<div id='three-preview-container'>
<preview.renderer />
Expand Down
35 changes: 11 additions & 24 deletions tests/integration/components/three-preview/renderer-test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import { find, render } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import { Mesh, BoxGeometry } from 'three';

module('Integration | Component | three-preview/renderer', function (hooks) {
setupRenderingTest(hooks);

for (const { parentSize, title } of [
{ parentSize: true, title: 'with parentSize true' },
{ parentSize: false, title: 'with parentSize false' },
]) {
test(`it handle parentSize attribute [${title}]`, async function (assert) {
this.set('parentSize', parentSize);
this.set('mesh', new Mesh(new BoxGeometry(12.12, 34.07, 56.42)));
test(`it render canvas at parent size`, async function (assert) {
this.set('mesh', new Mesh(new BoxGeometry(12.12, 34.07, 56.42)));

await render(hbs`<div style="width: 200px; height: 100px" data-test-renderer>
<ThreePreview::Renderer @mesh={{this.mesh}} @parentSize={{this.parentSize}} />
</div>`);
await render(hbs`<div style="width: 200px; height: 100px" data-test-renderer>
<ThreePreview::Renderer @mesh={{this.mesh}} @parentSize={{this.parentSize}} />
</div>`);

if (parentSize) {
assert
.dom('[data-test-renderer] canvas')
.hasAttribute('width', '200', 'Canvas width is adapted to parent size')
.hasAttribute('height', '100', 'Canvas height is adapted to parent size');
} else {
assert
.dom('[data-test-renderer] canvas')
.hasAttribute('width', '1024', 'Canvas width is 1024')
.hasAttribute('height', '768', 'Canvas height is 768');
}
});
}
const canvas = find('[data-test-renderer] canvas') as HTMLCanvasElement;

assert.strictEqual(canvas.offsetWidth, 200, 'Canvas width is adapted to parent size');
assert.strictEqual(canvas.offsetHeight, 100, 'Canvas height is adapted to parent size');
});
});

0 comments on commit 882e873

Please sign in to comment.