UNPKG

@arcgis/core

Version:

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

6 lines (5 loc) 4.64 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ import{_ as t}from"../../chunks/tslib.es6.js";import"../../core/has.js";import{wrapImageData as e}from"../../core/imageUtils.js";import a from"../../core/JSONSupport.js";import{property as r}from"../../core/accessorSupport/decorators/property.js";import{ensureClass as s}from"../../core/accessorSupport/ensureType.js";import"../../core/RandomLCG.js";import{reader as n}from"../../core/accessorSupport/decorators/reader.js";import{subclass as o}from"../../core/accessorSupport/decorators/subclass.js";import{writer as i}from"../../core/accessorSupport/decorators/writer.js";import{t as l,w as c}from"../../chunks/persistableUrlUtils.js";var p;const h=new WeakMap;let d=0,u=class extends a{static{p=this}constructor(t){super(t),this.wrap="repeat"}get url(){return this._get("url")||null}set url(t){this._set("url",t),t&&this._set("data",null)}get data(){return this._get("data")||null}set data(t){this._set("data",t),t&&this._set("url",null)}writeData(t,e,a,r){if(t instanceof HTMLImageElement){const s={type:"image-element",src:l(t.src,r),crossOrigin:t.crossOrigin};e[a]=s}else if(t instanceof HTMLCanvasElement){const r={type:"canvas-element",imageData:m(t.getContext("2d").getImageData(0,0,t.width,t.height))};e[a]=r}else if(t instanceof HTMLVideoElement){const s={type:"video-element",src:l(t.src,r),autoplay:t.autoplay,loop:t.loop,muted:t.muted,crossOrigin:t.crossOrigin,preload:t.preload};e[a]=s}else if(t instanceof ImageData){const r={type:"image-data",imageData:m(t)};e[a]=r}}readData(t){switch(t.type){case"image-element":{const e=new Image;return e.src=t.src,e.crossOrigin=t.crossOrigin,e}case"canvas-element":{const e=g(t.imageData),a=document.createElement("canvas");return a.width=e.width,a.height=e.height,a.getContext("2d").putImageData(e,0,0),a}case"image-data":return g(t.imageData);case"video-element":{const e=document.createElement("video");return e.src=t.src,e.crossOrigin=t.crossOrigin,e.autoplay=t.autoplay,e.loop=t.loop,e.muted=t.muted,e.preload=t.preload,e}default:return}}get transparent(){const{data:t,url:e}=this,a=t=>t?.toLowerCase().endsWith(".png")||t?.toLocaleLowerCase().startsWith("data:image/png;");return t instanceof HTMLCanvasElement?f(t.getContext("2d").getImageData(0,0,t.width,t.height)):t instanceof ImageData?f(t):!(!e||!a(e))||!!(t instanceof HTMLImageElement&&a(t.src))}set transparent(t){this._overrideIfSome("transparent",t)}get contentHash(){const t="string"==typeof this.wrap?this.wrap:"object"==typeof this.wrap?`${this.wrap.horizontal}/${this.wrap.vertical}`:"",e=(e="")=>`d:${e},t:${this.transparent},w:${t}`;return null!=this.url?e(this.url):null!=this.data?this.data instanceof HTMLImageElement||this.data instanceof HTMLVideoElement?e(this.data.src):(h.has(this.data)||h.set(this.data,++d),e(h.get(this.data))):e()}get memoryUsage(){let t=0;if(t+=null!=this.url?this.url.length:0,null!=this.data){const e=this.data;"data"in e?t+=e.data.byteLength:e instanceof HTMLImageElement?t+=e.naturalWidth*e.naturalHeight*3:e instanceof HTMLCanvasElement&&(t+=e.width*e.height*3)}return t}clone(){const t={url:this.url,data:this.data,transparent:this.transparent,wrap:this._cloneWrap()};return new p(t)}cloneWithDeduplication(t){const e=t.get(this);if(e)return e;const a=this.clone();return t.set(this,a),a}_cloneWrap(){return"string"==typeof this.wrap?this.wrap:{horizontal:this.wrap.horizontal,vertical:this.wrap.vertical}}static from(t){return"string"==typeof t?new p({url:t}):t instanceof HTMLImageElement||t instanceof HTMLCanvasElement||t instanceof ImageData||t instanceof HTMLVideoElement?new p({data:t}):s(p,t)}};function m(t){let e="";for(let a=0;a<t.data.length;a++)e+=String.fromCharCode(t.data[a]);return{data:btoa(e),width:t.width,height:t.height}}function g(t){const a=atob(t.data),r=new Uint8ClampedArray(a.length);for(let e=0;e<a.length;e++)r[e]=a.charCodeAt(e);return e(r,t.width,t.height)}function f(t){for(let e=3;e<t.data.length;e+=4)if(255!==t.data[e])return!0;return!1}t([r({type:String,json:{write:c}})],u.prototype,"url",null),t([r({json:{write:{overridePolicy(){return{enabled:!this.url}}}}}),r()],u.prototype,"data",null),t([i("data")],u.prototype,"writeData",null),t([n("data")],u.prototype,"readData",null),t([r({type:Boolean,json:{write:{overridePolicy(){return{enabled:this._isOverridden("transparent")}}}}})],u.prototype,"transparent",null),t([r({json:{write:!0}})],u.prototype,"wrap",void 0),t([r({readOnly:!0})],u.prototype,"contentHash",null),u=p=t([o("esri.geometry.support.MeshTexture")],u);export{u as default};