UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

6 lines (5 loc) 2.28 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.32/esri/copyright.txt for details. */ import e from"../../../core/Evented.js";import{on as t}from"../../../core/events.js";import{handlesGroup as a}from"../../../core/handleUtils.js";import has from"../../../core/has.js";import{deg2rad as r}from"../../../core/mathUtils.js";import{identity as s,scale as i,rotate as o,translate as n}from"../../../core/libs/gl-matrix-2/math/mat2d.js";import{create as h}from"../../../core/libs/gl-matrix-2/factories/mat2df64.js";class c{constructor(r){this.events=new e,this._hasMajorPerformanceCaveat=!1,this._lastRenderFrameCounter=0,this._canvas=document.createElement("canvas"),this._canvas.setAttribute("style","width: 100%; height:100%; display:block; willChange:transform");const s={failIfMajorPerformanceCaveat:!0,alpha:!0,antialias:!1,depth:!0,stencil:!0,powerPreference:"high-performance"};r.appendChild(this._canvas);let i=this._canvas.getContext("webgl2",s);i||(i=this._canvas.getContext("webgl2",{...s,failIfMajorPerformanceCaveat:!1}),this._hasMajorPerformanceCaveat=!0),this._gl=i,this._handles=a([t(this._canvas,"webglcontextlost",(e=>this.events.emit("webgl-context-lost",e)))])}destroy(){this._canvas.parentNode?.removeChild(this._canvas),this._canvas=null,this._handles.remove(),this._gl=null}get gl(){return this._gl}render(e,t){if(this._hasMajorPerformanceCaveat||has("esri-force-performance-mode")){if(++this._lastRenderFrameCounter>=has("esri-performance-mode-frames-between-render")&&(t(),this._lastRenderViewState=e.state.clone(),this._lastRenderFrameCounter=0),this._lastRenderViewState){const[t,a,r,s,i,o]=this._computeViewTransform(this._lastRenderViewState,e.state);this._canvas.style.transform=`matrix(${t}, ${a}, ${r}, ${s}, ${i}, ${o})`}}else t()}resize(e){const t=this._canvas,a=t.style,{state:{size:r},pixelRatio:s}=e,i=r[0],o=r[1],n=Math.round(i*s),h=Math.round(o*s);t.width===n&&t.height===h||(t.width=n,t.height=h),a.width=i+"px",a.height=o+"px"}_computeViewTransform(e,t){const[a,c]=e.center,[l,m]=t.center,[d,f]=e.toScreen([0,0],l,m),[_,p]=e.toScreen([0,0],a,c),v=_-d,g=p-f,w=e.scale/t.scale,u=t.rotation-e.rotation,C=h();return s(C),i(C,C,[w,w]),o(C,C,r(u)),n(C,C,[v,g]),C}}export{c as ManagedCanvas};