@@ -52,11 +52,19 @@ export type GCodePreviewOptions = {
5252 * @deprecated Please see the demo how to implement drag and drop.
5353 */
5454 allowDragNDrop ?: boolean ;
55+ /**
56+ * @deprecated Please use the `canvas` param instead.
57+ */
58+ targetId ?: string ;
5559 /** @experimental */
5660 devMode ?: boolean | DevModeOptions ;
5761} ;
5862
5963export class WebGLPreview {
64+ /**
65+ * @deprecated Please use the `canvas` param instead.
66+ */
67+ targetId ?: string ;
6068 scene : Scene ;
6169 camera : PerspectiveCamera ;
6270 renderer : WebGLRenderer ;
@@ -115,6 +123,7 @@ export class WebGLPreview {
115123 if ( opts . backgroundColor !== undefined ) {
116124 this . backgroundColor = new Color ( opts . backgroundColor ) ;
117125 }
126+ this . targetId = opts . targetId ;
118127 this . endLayer = opts . endLayer ;
119128 this . startLayer = opts . startLayer ;
120129 this . lineWidth = opts . lineWidth ?? 1 ;
@@ -156,11 +165,28 @@ export class WebGLPreview {
156165 console . info ( 'Using THREE r' + REVISION ) ;
157166 console . debug ( 'opts' , opts ) ;
158167
159- this . canvas = opts . canvas ;
160- this . renderer = new WebGLRenderer ( {
161- canvas : this . canvas ,
162- preserveDrawingBuffer : true
163- } ) ;
168+ if ( this . targetId ) {
169+ console . warn ( '`targetId` is deprecated and will removed in the future. Use `canvas` instead.' ) ;
170+ }
171+
172+ if ( ! opts . canvas ) {
173+ if ( ! this . targetId ) {
174+ throw Error ( 'Set either opts.canvas or opts.targetId' ) ;
175+ }
176+ const container = document . getElementById ( this . targetId ) ;
177+ if ( ! container ) throw new Error ( 'Unable to find element ' + this . targetId ) ;
178+
179+ this . renderer = new WebGLRenderer ( { preserveDrawingBuffer : true } ) ;
180+ this . canvas = this . renderer . domElement ;
181+
182+ container . appendChild ( this . canvas ) ;
183+ } else {
184+ this . canvas = opts . canvas ;
185+ this . renderer = new WebGLRenderer ( {
186+ canvas : this . canvas ,
187+ preserveDrawingBuffer : true
188+ } ) ;
189+ }
164190
165191 this . camera = new PerspectiveCamera ( 25 , this . canvas . offsetWidth / this . canvas . offsetHeight , 10 , 5000 ) ;
166192 this . camera . position . fromArray ( this . initialCameraPosition ) ;
0 commit comments