您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. /**
  2. * @Author: Caven
  3. * @Date: 2021-05-01 17:38:47
  4. */
  5. const __indexOf =
  6. [].indexOf ||
  7. function(item) {
  8. for (let i = 0, l = this.length; i < l; i++) {
  9. if (i in this && this[i] === item) return i
  10. }
  11. return -1
  12. }
  13. const vertexShaderBlit =
  14. 'attribute vec4 position;\nvarying vec2 texcoord;\nvoid main(){\n texcoord = position.xy*0.5+0.5;\n gl_Position = position;\n}'
  15. const fragmentShaderBlit =
  16. '#ifdef GL_FRAGMENT_PRECISION_HIGH\n precision highp int;\n precision highp float;\n#else\n precision mediump int;\n precision mediump float;\n#endif\nuniform sampler2D source;\nvarying vec2 texcoord;'
  17. const nukeVendorPrefix = function() {
  18. let getExtension, getSupportedExtensions, vendorRe, vendors
  19. if (window.WebGLRenderingContext != null) {
  20. vendors = ['WEBKIT', 'MOZ', 'MS', 'O']
  21. vendorRe = /^WEBKIT_(.*)|MOZ_(.*)|MS_(.*)|O_(.*)/
  22. getExtension = WebGLRenderingContext.prototype.getExtension
  23. WebGLRenderingContext.prototype.getExtension = function(name) {
  24. let extobj, match, vendor, _i, _len
  25. match = name.match(vendorRe)
  26. if (match !== null) {
  27. name = match[1]
  28. }
  29. extobj = getExtension.call(this, name)
  30. if (extobj === null) {
  31. for (_i = 0, _len = vendors.length; _i < _len; _i++) {
  32. vendor = vendors[_i]
  33. extobj = getExtension.call(this, vendor + '_' + name)
  34. if (extobj !== null) {
  35. return extobj
  36. }
  37. }
  38. return null
  39. } else {
  40. return extobj
  41. }
  42. }
  43. getSupportedExtensions =
  44. WebGLRenderingContext.prototype.getSupportedExtensions
  45. return (WebGLRenderingContext.prototype.getSupportedExtensions = function() {
  46. let extension, match, result, supported, _i, _len
  47. supported = getSupportedExtensions.call(this)
  48. result = []
  49. for (_i = 0, _len = supported.length; _i < _len; _i++) {
  50. extension = supported[_i]
  51. match = extension.match(vendorRe)
  52. if (match !== null) {
  53. extension = match[1]
  54. }
  55. if (__indexOf.call(result, extension) < 0) {
  56. result.push(extension)
  57. }
  58. }
  59. return result
  60. })
  61. }
  62. }
  63. const textureFloatShims = function() {
  64. let checkColorBuffer,
  65. checkFloatLinear,
  66. checkSupport,
  67. checkTexture,
  68. createSourceCanvas,
  69. getExtension,
  70. getSupportedExtensions,
  71. name,
  72. shimExtensions,
  73. shimLookup,
  74. unshimExtensions,
  75. unshimLookup,
  76. _i,
  77. _len
  78. createSourceCanvas = function() {
  79. let canvas, ctx, imageData
  80. canvas = document.createElement('canvas')
  81. canvas.width = 2
  82. canvas.height = 2
  83. ctx = canvas.getContext('2d')
  84. imageData = ctx.getImageData(0, 0, 2, 2)
  85. imageData.data.set(
  86. new Uint8ClampedArray([
  87. 0,
  88. 0,
  89. 0,
  90. 0,
  91. 255,
  92. 255,
  93. 255,
  94. 255,
  95. 0,
  96. 0,
  97. 0,
  98. 0,
  99. 255,
  100. 255,
  101. 255,
  102. 255
  103. ])
  104. )
  105. ctx.putImageData(imageData, 0, 0)
  106. return canvas
  107. }
  108. createSourceCanvas()
  109. checkFloatLinear = function(gl, sourceType) {
  110. let buffer,
  111. cleanup,
  112. fragmentShader,
  113. framebuffer,
  114. positionLoc,
  115. program,
  116. readBuffer,
  117. result,
  118. source,
  119. sourceCanvas,
  120. sourceLoc,
  121. target,
  122. vertexShader,
  123. vertices
  124. program = gl.createProgram()
  125. vertexShader = gl.createShader(gl.VERTEX_SHADER)
  126. gl.attachShader(program, vertexShader)
  127. gl.shaderSource(
  128. vertexShader,
  129. 'attribute vec2 position;\nvoid main(){\n gl_Position = vec4(position, 0.0, 1.0);\n}'
  130. )
  131. gl.compileShader(vertexShader)
  132. if (!gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS)) {
  133. throw gl.getShaderInfoLog(vertexShader)
  134. }
  135. fragmentShader = gl.createShader(gl.FRAGMENT_SHADER)
  136. gl.attachShader(program, fragmentShader)
  137. gl.shaderSource(
  138. fragmentShader,
  139. 'uniform sampler2D source;\nvoid main(){\n gl_FragColor = texture2D(source, vec2(1.0, 1.0));\n}'
  140. )
  141. gl.compileShader(fragmentShader)
  142. if (!gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS)) {
  143. throw gl.getShaderInfoLog(fragmentShader)
  144. }
  145. gl.linkProgram(program)
  146. if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
  147. throw gl.getProgramInfoLog(program)
  148. }
  149. gl.useProgram(program)
  150. cleanup = function() {
  151. gl.deleteShader(fragmentShader)
  152. gl.deleteShader(vertexShader)
  153. gl.deleteProgram(program)
  154. gl.deleteBuffer(buffer)
  155. gl.deleteTexture(source)
  156. gl.deleteTexture(target)
  157. gl.deleteFramebuffer(framebuffer)
  158. gl.bindBuffer(gl.ARRAY_BUFFER, null)
  159. gl.useProgram(null)
  160. gl.bindTexture(gl.TEXTURE_2D, null)
  161. return gl.bindFramebuffer(gl.FRAMEBUFFER, null)
  162. }
  163. target = gl.createTexture()
  164. gl.bindTexture(gl.TEXTURE_2D, target)
  165. gl.texImage2D(
  166. gl.TEXTURE_2D,
  167. 0,
  168. gl.RGBA,
  169. 2,
  170. 2,
  171. 0,
  172. gl.RGBA,
  173. gl.UNSIGNED_BYTE,
  174. null
  175. )
  176. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
  177. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
  178. framebuffer = gl.createFramebuffer()
  179. gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer)
  180. gl.framebufferTexture2D(
  181. gl.FRAMEBUFFER,
  182. gl.COLOR_ATTACHMENT0,
  183. gl.TEXTURE_2D,
  184. target,
  185. 0
  186. )
  187. sourceCanvas = createSourceCanvas()
  188. source = gl.createTexture()
  189. gl.bindTexture(gl.TEXTURE_2D, source)
  190. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, sourceType, sourceCanvas)
  191. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
  192. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
  193. vertices = new Float32Array([1, 1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1])
  194. buffer = gl.createBuffer()
  195. gl.bindBuffer(gl.ARRAY_BUFFER, buffer)
  196. gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW)
  197. positionLoc = gl.getAttribLocation(program, 'position')
  198. sourceLoc = gl.getUniformLocation(program, 'source')
  199. gl.enableVertexAttribArray(positionLoc)
  200. gl.vertexAttribPointer(positionLoc, 2, gl.FLOAT, false, 0, 0)
  201. gl.uniform1i(sourceLoc, 0)
  202. gl.drawArrays(gl.TRIANGLES, 0, 6)
  203. readBuffer = new Uint8Array(4 * 4)
  204. gl.readPixels(0, 0, 2, 2, gl.RGBA, gl.UNSIGNED_BYTE, readBuffer)
  205. result = Math.abs(readBuffer[0] - 127) < 10
  206. cleanup()
  207. return result
  208. }
  209. checkTexture = function(gl, targetType) {
  210. let target
  211. target = gl.createTexture()
  212. gl.bindTexture(gl.TEXTURE_2D, target)
  213. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 2, 2, 0, gl.RGBA, targetType, null)
  214. if (gl.getError() === 0) {
  215. gl.deleteTexture(target)
  216. return true
  217. } else {
  218. gl.deleteTexture(target)
  219. return false
  220. }
  221. }
  222. checkColorBuffer = function(gl, targetType) {
  223. let check, framebuffer, target
  224. target = gl.createTexture()
  225. gl.bindTexture(gl.TEXTURE_2D, target)
  226. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 2, 2, 0, gl.RGBA, targetType, null)
  227. framebuffer = gl.createFramebuffer()
  228. gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer)
  229. gl.framebufferTexture2D(
  230. gl.FRAMEBUFFER,
  231. gl.COLOR_ATTACHMENT0,
  232. gl.TEXTURE_2D,
  233. target,
  234. 0
  235. )
  236. check = gl.checkFramebufferStatus(gl.FRAMEBUFFER)
  237. gl.deleteTexture(target)
  238. gl.deleteFramebuffer(framebuffer)
  239. gl.bindTexture(gl.TEXTURE_2D, null)
  240. gl.bindFramebuffer(gl.FRAMEBUFFER, null)
  241. if (check === gl.FRAMEBUFFER_COMPLETE) {
  242. return true
  243. } else {
  244. return false
  245. }
  246. }
  247. shimExtensions = []
  248. shimLookup = {}
  249. unshimExtensions = []
  250. checkSupport = function() {
  251. let canvas,
  252. extobj,
  253. gl,
  254. halfFloatExt,
  255. halfFloatTexturing,
  256. singleFloatExt,
  257. singleFloatTexturing
  258. canvas = document.createElement('canvas')
  259. gl = null
  260. try {
  261. gl = canvas.getContext('experimental-webgl')
  262. if (gl === null) {
  263. gl = canvas.getContext('webgl')
  264. }
  265. // eslint-disable-next-line no-empty
  266. } catch (_error) {}
  267. if (gl != null) {
  268. singleFloatExt = gl.getExtension('OES_texture_float')
  269. if (singleFloatExt === null) {
  270. if (checkTexture(gl, gl.FLOAT)) {
  271. singleFloatTexturing = true
  272. shimExtensions.push('OES_texture_float')
  273. shimLookup.OES_texture_float = {
  274. shim: true
  275. }
  276. } else {
  277. singleFloatTexturing = false
  278. unshimExtensions.push('OES_texture_float')
  279. }
  280. } else {
  281. if (checkTexture(gl, gl.FLOAT)) {
  282. singleFloatTexturing = true
  283. shimExtensions.push('OES_texture_float')
  284. } else {
  285. singleFloatTexturing = false
  286. unshimExtensions.push('OES_texture_float')
  287. }
  288. }
  289. if (singleFloatTexturing) {
  290. extobj = gl.getExtension('WEBGL_color_buffer_float')
  291. if (extobj === null) {
  292. if (checkColorBuffer(gl, gl.FLOAT)) {
  293. shimExtensions.push('WEBGL_color_buffer_float')
  294. shimLookup.WEBGL_color_buffer_float = {
  295. shim: true,
  296. RGBA32F_EXT: 0x8814,
  297. RGB32F_EXT: 0x8815,
  298. FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE_EXT: 0x8211,
  299. UNSIGNED_NORMALIZED_EXT: 0x8c17
  300. }
  301. } else {
  302. unshimExtensions.push('WEBGL_color_buffer_float')
  303. }
  304. } else {
  305. if (checkColorBuffer(gl, gl.FLOAT)) {
  306. shimExtensions.push('WEBGL_color_buffer_float')
  307. } else {
  308. unshimExtensions.push('WEBGL_color_buffer_float')
  309. }
  310. }
  311. extobj = gl.getExtension('OES_texture_float_linear')
  312. if (extobj === null) {
  313. if (checkFloatLinear(gl, gl.FLOAT)) {
  314. shimExtensions.push('OES_texture_float_linear')
  315. shimLookup.OES_texture_float_linear = {
  316. shim: true
  317. }
  318. } else {
  319. unshimExtensions.push('OES_texture_float_linear')
  320. }
  321. } else {
  322. if (checkFloatLinear(gl, gl.FLOAT)) {
  323. shimExtensions.push('OES_texture_float_linear')
  324. } else {
  325. unshimExtensions.push('OES_texture_float_linear')
  326. }
  327. }
  328. }
  329. halfFloatExt = gl.getExtension('OES_texture_half_float')
  330. if (halfFloatExt === null) {
  331. if (checkTexture(gl, 0x8d61)) {
  332. halfFloatTexturing = true
  333. shimExtensions.push('OES_texture_half_float')
  334. halfFloatExt = shimLookup.OES_texture_half_float = {
  335. HALF_FLOAT_OES: 0x8d61,
  336. shim: true
  337. }
  338. } else {
  339. halfFloatTexturing = false
  340. unshimExtensions.push('OES_texture_half_float')
  341. }
  342. } else {
  343. if (checkTexture(gl, halfFloatExt.HALF_FLOAT_OES)) {
  344. halfFloatTexturing = true
  345. shimExtensions.push('OES_texture_half_float')
  346. } else {
  347. halfFloatTexturing = false
  348. unshimExtensions.push('OES_texture_half_float')
  349. }
  350. }
  351. if (halfFloatTexturing) {
  352. extobj = gl.getExtension('EXT_color_buffer_half_float')
  353. if (extobj === null) {
  354. if (checkColorBuffer(gl, halfFloatExt.HALF_FLOAT_OES)) {
  355. shimExtensions.push('EXT_color_buffer_half_float')
  356. shimLookup.EXT_color_buffer_half_float = {
  357. shim: true,
  358. RGBA16F_EXT: 0x881a,
  359. RGB16F_EXT: 0x881b,
  360. FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE_EXT: 0x8211,
  361. UNSIGNED_NORMALIZED_EXT: 0x8c17
  362. }
  363. } else {
  364. unshimExtensions.push('EXT_color_buffer_half_float')
  365. }
  366. } else {
  367. if (checkColorBuffer(gl, halfFloatExt.HALF_FLOAT_OES)) {
  368. shimExtensions.push('EXT_color_buffer_half_float')
  369. } else {
  370. unshimExtensions.push('EXT_color_buffer_half_float')
  371. }
  372. }
  373. extobj = gl.getExtension('OES_texture_half_float_linear')
  374. if (extobj === null) {
  375. if (checkFloatLinear(gl, halfFloatExt.HALF_FLOAT_OES)) {
  376. shimExtensions.push('OES_texture_half_float_linear')
  377. return (shimLookup.OES_texture_half_float_linear = {
  378. shim: true
  379. })
  380. } else {
  381. return unshimExtensions.push('OES_texture_half_float_linear')
  382. }
  383. } else {
  384. if (checkFloatLinear(gl, halfFloatExt.HALF_FLOAT_OES)) {
  385. return shimExtensions.push('OES_texture_half_float_linear')
  386. } else {
  387. return unshimExtensions.push('OES_texture_half_float_linear')
  388. }
  389. }
  390. }
  391. }
  392. }
  393. if (window.WebGLRenderingContext != null) {
  394. checkSupport()
  395. unshimLookup = {}
  396. for (_i = 0, _len = unshimExtensions.length; _i < _len; _i++) {
  397. name = unshimExtensions[_i]
  398. unshimLookup[name] = true
  399. }
  400. getExtension = WebGLRenderingContext.prototype.getExtension
  401. WebGLRenderingContext.prototype.getExtension = function(name) {
  402. let extobj
  403. extobj = shimLookup[name]
  404. if (extobj === void 0) {
  405. if (unshimLookup[name]) {
  406. return null
  407. } else {
  408. return getExtension.call(this, name)
  409. }
  410. } else {
  411. return extobj
  412. }
  413. }
  414. getSupportedExtensions =
  415. WebGLRenderingContext.prototype.getSupportedExtensions
  416. WebGLRenderingContext.prototype.getSupportedExtensions = function() {
  417. let extension, result, supported, _j, _k, _len1, _len2
  418. supported = getSupportedExtensions.call(this)
  419. result = []
  420. for (_j = 0, _len1 = supported.length; _j < _len1; _j++) {
  421. extension = supported[_j]
  422. if (unshimLookup[extension] === void 0) {
  423. result.push(extension)
  424. }
  425. }
  426. for (_k = 0, _len2 = shimExtensions.length; _k < _len2; _k++) {
  427. extension = shimExtensions[_k]
  428. if (__indexOf.call(result, extension) < 0) {
  429. result.push(extension)
  430. }
  431. }
  432. return result
  433. }
  434. return (WebGLRenderingContext.prototype.getFloatExtension = function(spec) {
  435. let candidate,
  436. candidates,
  437. half,
  438. halfFramebuffer,
  439. halfLinear,
  440. halfTexture,
  441. i,
  442. importance,
  443. preference,
  444. result,
  445. single,
  446. singleFramebuffer,
  447. singleLinear,
  448. singleTexture,
  449. use,
  450. _j,
  451. _k,
  452. _l,
  453. _len1,
  454. _len2,
  455. _len3,
  456. _len4,
  457. _m,
  458. _ref,
  459. _ref1,
  460. _ref2
  461. if (spec.prefer == null) {
  462. spec.prefer = ['half']
  463. }
  464. if (spec.require == null) {
  465. spec.require = []
  466. }
  467. if (spec.throws == null) {
  468. spec.throws = true
  469. }
  470. singleTexture = this.getExtension('OES_texture_float')
  471. halfTexture = this.getExtension('OES_texture_half_float')
  472. singleFramebuffer = this.getExtension('WEBGL_color_buffer_float')
  473. halfFramebuffer = this.getExtension('EXT_color_buffer_half_float')
  474. singleLinear = this.getExtension('OES_texture_float_linear')
  475. halfLinear = this.getExtension('OES_texture_half_float_linear')
  476. single = {
  477. texture: singleTexture !== null,
  478. filterable: singleLinear !== null,
  479. renderable: singleFramebuffer !== null,
  480. score: 0,
  481. precision: 'single',
  482. half: false,
  483. single: true,
  484. type: this.FLOAT
  485. }
  486. half = {
  487. texture: halfTexture !== null,
  488. filterable: halfLinear !== null,
  489. renderable: halfFramebuffer !== null,
  490. score: 0,
  491. precision: 'half',
  492. half: true,
  493. single: false,
  494. type:
  495. (_ref = halfTexture != null ? halfTexture.HALF_FLOAT_OES : void 0) !=
  496. null
  497. ? _ref
  498. : null
  499. }
  500. candidates = []
  501. if (single.texture) {
  502. candidates.push(single)
  503. }
  504. if (half.texture) {
  505. candidates.push(half)
  506. }
  507. result = []
  508. for (_j = 0, _len1 = candidates.length; _j < _len1; _j++) {
  509. candidate = candidates[_j]
  510. use = true
  511. _ref1 = spec.require
  512. for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) {
  513. name = _ref1[_k]
  514. if (candidate[name] === false) {
  515. use = false
  516. }
  517. }
  518. if (use) {
  519. result.push(candidate)
  520. }
  521. }
  522. for (_l = 0, _len3 = result.length; _l < _len3; _l++) {
  523. candidate = result[_l]
  524. _ref2 = spec.prefer
  525. for (i = _m = 0, _len4 = _ref2.length; _m < _len4; i = ++_m) {
  526. preference = _ref2[i]
  527. importance = Math.pow(2, spec.prefer.length - i - 1)
  528. if (candidate[preference]) {
  529. candidate.score += importance
  530. }
  531. }
  532. }
  533. result.sort(function(a, b) {
  534. if (a.score === b.score) {
  535. return 0
  536. } else if (a.score < b.score) {
  537. return 1
  538. } else if (a.score > b.score) {
  539. return -1
  540. }
  541. })
  542. if (result.length === 0) {
  543. if (spec.throws) {
  544. throw 'No floating point texture support that is ' +
  545. spec.require.join(', ')
  546. } else {
  547. return null
  548. }
  549. } else {
  550. result = result[0]
  551. return {
  552. filterable: result.filterable,
  553. renderable: result.renderable,
  554. type: result.type,
  555. precision: result.precision
  556. }
  557. }
  558. })
  559. }
  560. }
  561. nukeVendorPrefix()
  562. textureFloatShims()
  563. const Shader = (function() {
  564. function Shader(gl, _arg) {
  565. let fragment, vertex
  566. this.gl = gl
  567. vertex = _arg.vertex
  568. fragment = _arg.fragment
  569. this.program = this.gl.createProgram()
  570. this.vs = this.gl.createShader(this.gl.VERTEX_SHADER)
  571. this.fs = this.gl.createShader(this.gl.FRAGMENT_SHADER)
  572. this.gl.attachShader(this.program, this.vs)
  573. this.gl.attachShader(this.program, this.fs)
  574. this.compileShader(this.vs, vertex)
  575. this.compileShader(this.fs, fragment)
  576. this.link()
  577. this.value_cache = {}
  578. this.uniform_cache = {}
  579. this.attribCache = {}
  580. }
  581. Shader.prototype.attribLocation = function(name) {
  582. let location = this.attribCache[name]
  583. if (location === void 0) {
  584. location = this.attribCache[name] = this.gl.getAttribLocation(
  585. this.program,
  586. name
  587. )
  588. }
  589. return location
  590. }
  591. Shader.prototype.compileShader = function(shader, source) {
  592. this.gl.shaderSource(shader, source)
  593. this.gl.compileShader(shader)
  594. if (!this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS)) {
  595. throw 'Shader Compile Error: ' + this.gl.getShaderInfoLog(shader)
  596. }
  597. }
  598. Shader.prototype.link = function() {
  599. this.gl.linkProgram(this.program)
  600. if (!this.gl.getProgramParameter(this.program, this.gl.LINK_STATUS)) {
  601. throw 'Shader Link Error: ' + this.gl.getProgramInfoLog(this.program)
  602. }
  603. }
  604. Shader.prototype.use = function() {
  605. this.gl.useProgram(this.program)
  606. return this
  607. }
  608. Shader.prototype.uniformLoc = function(name) {
  609. let location = this.uniform_cache[name]
  610. if (location === void 0) {
  611. location = this.uniform_cache[name] = this.gl.getUniformLocation(
  612. this.program,
  613. name
  614. )
  615. }
  616. return location
  617. }
  618. Shader.prototype.int = function(name, value) {
  619. let cached, loc
  620. cached = this.value_cache[name]
  621. if (cached !== value) {
  622. this.value_cache[name] = value
  623. loc = this.uniformLoc(name)
  624. if (loc) {
  625. this.gl.uniform1i(loc, value)
  626. }
  627. }
  628. return this
  629. }
  630. Shader.prototype.vec2 = function(name, a, b) {
  631. let loc
  632. loc = this.uniformLoc(name)
  633. if (loc) {
  634. this.gl.uniform2f(loc, a, b)
  635. }
  636. return this
  637. }
  638. Shader.prototype.float = function(name, value) {
  639. let cached, loc
  640. cached = this.value_cache[name]
  641. if (cached !== value) {
  642. this.value_cache[name] = value
  643. loc = this.uniformLoc(name)
  644. if (loc) {
  645. this.gl.uniform1f(loc, value)
  646. }
  647. }
  648. return this
  649. }
  650. return Shader
  651. })()
  652. const Framebuffer = (function() {
  653. function Framebuffer(gl) {
  654. this.gl = gl
  655. this.buffer = this.gl.createFramebuffer()
  656. }
  657. Framebuffer.prototype.destroy = function() {
  658. return this.gl.deleteFRamebuffer(this.buffer)
  659. }
  660. Framebuffer.prototype.bind = function() {
  661. this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, this.buffer)
  662. return this
  663. }
  664. Framebuffer.prototype.unbind = function() {
  665. this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, null)
  666. return this
  667. }
  668. Framebuffer.prototype.check = function() {
  669. let result
  670. result = this.gl.checkFramebufferStatus(this.gl.FRAMEBUFFER)
  671. switch (result) {
  672. case this.gl.FRAMEBUFFER_UNSUPPORTED:
  673. throw 'Framebuffer is unsupported'
  674. // eslint-disable-next-line no-unreachable
  675. break
  676. case this.gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
  677. throw 'Framebuffer incomplete attachment'
  678. // eslint-disable-next-line no-unreachable
  679. break
  680. case this.gl.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
  681. throw 'Framebuffer incomplete dimensions'
  682. // eslint-disable-next-line no-unreachable
  683. break
  684. case this.gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
  685. throw 'Framebuffer incomplete missing attachment'
  686. }
  687. return this
  688. }
  689. Framebuffer.prototype.color = function(texture) {
  690. this.gl.framebufferTexture2D(
  691. this.gl.FRAMEBUFFER,
  692. this.gl.COLOR_ATTACHMENT0,
  693. texture.target,
  694. texture.handle,
  695. 0
  696. )
  697. this.check()
  698. return this
  699. }
  700. Framebuffer.prototype.depth = function(buffer) {
  701. this.gl.framebufferRenderbuffer(
  702. this.gl.FRAMEBUFFER,
  703. this.gl.DEPTH_ATTACHMENT,
  704. this.gl.RENDERBUFFER,
  705. buffer.id
  706. )
  707. this.check()
  708. return this
  709. }
  710. Framebuffer.prototype.destroy = function() {
  711. return this.gl.deleteFramebuffer(this.buffer)
  712. }
  713. return Framebuffer
  714. })()
  715. const Texture = (function() {
  716. function Texture(gl, params) {
  717. let _ref, _ref1
  718. this.gl = gl
  719. if (params == null) {
  720. params = {}
  721. }
  722. this.channels = this.gl[
  723. ((_ref = params.channels) != null ? _ref : 'rgba').toUpperCase()
  724. ]
  725. if (typeof params.type === 'number') {
  726. this.type = params.type
  727. } else {
  728. this.type = this.gl[
  729. ((_ref1 = params.type) != null ? _ref1 : 'unsigned_byte').toUpperCase()
  730. ]
  731. }
  732. switch (this.channels) {
  733. case this.gl.RGBA:
  734. this.chancount = 4
  735. break
  736. case this.gl.RGB:
  737. this.chancount = 3
  738. break
  739. case this.gl.LUMINANCE_ALPHA:
  740. this.chancount = 2
  741. break
  742. default:
  743. this.chancount = 1
  744. }
  745. this.target = this.gl.TEXTURE_2D
  746. this.handle = this.gl.createTexture()
  747. }
  748. Texture.prototype.destroy = function() {
  749. return this.gl.deleteTexture(this.handle)
  750. }
  751. Texture.prototype.bind = function(unit) {
  752. if (unit == null) {
  753. unit = 0
  754. }
  755. if (unit > 15) {
  756. throw 'Texture unit too large: ' + unit
  757. }
  758. this.gl.activeTexture(this.gl.TEXTURE0 + unit)
  759. this.gl.bindTexture(this.target, this.handle)
  760. return this
  761. }
  762. Texture.prototype.setSize = function(width, height) {
  763. this.width = width
  764. this.height = height
  765. this.gl.texImage2D(
  766. this.target,
  767. 0,
  768. this.channels,
  769. this.width,
  770. this.height,
  771. 0,
  772. this.channels,
  773. this.type,
  774. null
  775. )
  776. return this
  777. }
  778. Texture.prototype.upload = function(data) {
  779. this.width = data.width
  780. this.height = data.height
  781. this.gl.texImage2D(
  782. this.target,
  783. 0,
  784. this.channels,
  785. this.channels,
  786. this.type,
  787. data
  788. )
  789. return this
  790. }
  791. Texture.prototype.linear = function() {
  792. this.gl.texParameteri(
  793. this.target,
  794. this.gl.TEXTURE_MAG_FILTER,
  795. this.gl.LINEAR
  796. )
  797. this.gl.texParameteri(
  798. this.target,
  799. this.gl.TEXTURE_MIN_FILTER,
  800. this.gl.LINEAR
  801. )
  802. return this
  803. }
  804. Texture.prototype.nearest = function() {
  805. this.gl.texParameteri(
  806. this.target,
  807. this.gl.TEXTURE_MAG_FILTER,
  808. this.gl.NEAREST
  809. )
  810. this.gl.texParameteri(
  811. this.target,
  812. this.gl.TEXTURE_MIN_FILTER,
  813. this.gl.NEAREST
  814. )
  815. return this
  816. }
  817. Texture.prototype.clampToEdge = function() {
  818. this.gl.texParameteri(
  819. this.target,
  820. this.gl.TEXTURE_WRAP_S,
  821. this.gl.CLAMP_TO_EDGE
  822. )
  823. this.gl.texParameteri(
  824. this.target,
  825. this.gl.TEXTURE_WRAP_T,
  826. this.gl.CLAMP_TO_EDGE
  827. )
  828. return this
  829. }
  830. Texture.prototype.repeat = function() {
  831. this.gl.texParameteri(this.target, this.gl.TEXTURE_WRAP_S, this.gl.REPEAT)
  832. this.gl.texParameteri(this.target, this.gl.TEXTURE_WRAP_T, this.gl.REPEAT)
  833. return this
  834. }
  835. return Texture
  836. })()
  837. const Node = (function() {
  838. function Node(gl, width, height) {
  839. let floatExt
  840. this.gl = gl
  841. this.width = width
  842. this.height = height
  843. floatExt = this.gl.getFloatExtension({
  844. require: ['renderable']
  845. })
  846. this.texture = new Texture(this.gl, {
  847. type: floatExt.type
  848. })
  849. .bind(0)
  850. .setSize(this.width, this.height)
  851. .nearest()
  852. .clampToEdge()
  853. this.fbo = new Framebuffer(this.gl)
  854. .bind()
  855. .color(this.texture)
  856. .unbind()
  857. }
  858. Node.prototype.use = function() {
  859. return this.fbo.bind()
  860. }
  861. Node.prototype.bind = function(unit) {
  862. return this.texture.bind(unit)
  863. }
  864. Node.prototype.end = function() {
  865. return this.fbo.unbind()
  866. }
  867. Node.prototype.resize = function(width, height) {
  868. this.width = width
  869. this.height = height
  870. return this.texture.bind(0).setSize(this.width, this.height)
  871. }
  872. return Node
  873. })()
  874. const Heights = (function() {
  875. function Heights(heatmap, gl, width, height) {
  876. let i, _i, _ref
  877. this.heatmap = heatmap
  878. this.gl = gl
  879. this.width = width
  880. this.height = height
  881. this.shader = new Shader(this.gl, {
  882. vertex:
  883. 'attribute vec4 position, intensity;\nvarying vec2 off, dim;\nvarying float vIntensity;\nuniform vec2 viewport;\n\nvoid main(){\n dim = abs(position.zw);\n off = position.zw;\n vec2 pos = position.xy + position.zw;\n vIntensity = intensity.x;\n gl_Position = vec4((pos/viewport)*2.0-1.0, 0.0, 1.0);\n}',
  884. fragment:
  885. '#ifdef GL_FRAGMENT_PRECISION_HIGH\n precision highp int;\n precision highp float;\n#else\n precision mediump int;\n precision mediump float;\n#endif\nvarying vec2 off, dim;\nvarying float vIntensity;\nvoid main(){\n float falloff = (1.0 - smoothstep(0.0, 1.0, length(off/dim)));\n float intensity = falloff*vIntensity;\n gl_FragColor = vec4(intensity);\n}'
  886. })
  887. this.clampShader = new Shader(this.gl, {
  888. vertex: vertexShaderBlit,
  889. fragment:
  890. fragmentShaderBlit +
  891. 'uniform float low, high;\nvoid main(){\n gl_FragColor = vec4(clamp(texture2D(source, texcoord).rgb, low, high), 1.0);\n}'
  892. })
  893. this.multiplyShader = new Shader(this.gl, {
  894. vertex: vertexShaderBlit,
  895. fragment:
  896. fragmentShaderBlit +
  897. 'uniform float value;\nvoid main(){\n gl_FragColor = vec4(texture2D(source, texcoord).rgb*value, 1.0);\n}'
  898. })
  899. this.blurShader = new Shader(this.gl, {
  900. vertex: vertexShaderBlit,
  901. fragment:
  902. fragmentShaderBlit +
  903. 'uniform vec2 viewport;\nvoid main(){\n vec4 result = vec4(0.0);\n for(int x=-1; x<=1; x++){\n for(int y=-1; y<=1; y++){\n vec2 off = vec2(x,y)/viewport;\n //float factor = 1.0 - smoothstep(0.0, 1.5, length(off));\n float factor = 1.0;\n result += vec4(texture2D(source, texcoord+off).rgb*factor, factor);\n }\n }\n gl_FragColor = vec4(result.rgb/result.w, 1.0);\n}'
  904. })
  905. this.nodeBack = new Node(this.gl, this.width, this.height)
  906. this.nodeFront = new Node(this.gl, this.width, this.height)
  907. this.vertexBuffer = this.gl.createBuffer()
  908. this.vertexSize = 8
  909. this.maxPointCount = 1024 * 10
  910. this.vertexBufferData = new Float32Array(
  911. this.maxPointCount * this.vertexSize * 6
  912. )
  913. this.vertexBufferViews = []
  914. for (
  915. i = _i = 0, _ref = this.maxPointCount;
  916. 0 <= _ref ? _i < _ref : _i > _ref;
  917. i = 0 <= _ref ? ++_i : --_i
  918. ) {
  919. this.vertexBufferViews.push(
  920. new Float32Array(
  921. this.vertexBufferData.buffer,
  922. 0,
  923. i * this.vertexSize * 6
  924. )
  925. )
  926. }
  927. this.bufferIndex = 0
  928. this.pointCount = 0
  929. }
  930. Heights.prototype.resize = function(width, height) {
  931. this.width = width
  932. this.height = height
  933. this.nodeBack.resize(this.width, this.height)
  934. return this.nodeFront.resize(this.width, this.height)
  935. }
  936. Heights.prototype.update = function() {
  937. var intensityLoc, positionLoc
  938. if (this.pointCount > 0) {
  939. this.gl.enable(this.gl.BLEND)
  940. this.nodeFront.use()
  941. this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.vertexBuffer)
  942. this.gl.bufferData(
  943. this.gl.ARRAY_BUFFER,
  944. this.vertexBufferViews[this.pointCount],
  945. this.gl.STREAM_DRAW
  946. )
  947. positionLoc = this.shader.attribLocation('position')
  948. intensityLoc = this.shader.attribLocation('intensity')
  949. this.gl.enableVertexAttribArray(1)
  950. this.gl.vertexAttribPointer(
  951. positionLoc,
  952. 4,
  953. this.gl.FLOAT,
  954. false,
  955. 8 * 4,
  956. 0
  957. )
  958. this.gl.vertexAttribPointer(
  959. intensityLoc,
  960. 4,
  961. this.gl.FLOAT,
  962. false,
  963. 8 * 4,
  964. 4 * 4
  965. )
  966. this.shader.use().vec2('viewport', this.width, this.height)
  967. this.gl.drawArrays(this.gl.TRIANGLES, 0, this.pointCount * 6)
  968. this.gl.disableVertexAttribArray(1)
  969. this.pointCount = 0
  970. this.bufferIndex = 0
  971. this.nodeFront.end()
  972. return this.gl.disable(this.gl.BLEND)
  973. }
  974. }
  975. Heights.prototype.clear = function() {
  976. this.nodeFront.use()
  977. this.gl.clearColor(0, 0, 0, 1)
  978. this.gl.clear(this.gl.COLOR_BUFFER_BIT)
  979. return this.nodeFront.end()
  980. }
  981. Heights.prototype.clamp = function(min, max) {
  982. this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.heatmap.quad)
  983. this.gl.vertexAttribPointer(0, 4, this.gl.FLOAT, false, 0, 0)
  984. this.nodeFront.bind(0)
  985. this.nodeBack.use()
  986. this.clampShader
  987. .use()
  988. .int('source', 0)
  989. .float('low', min)
  990. .float('high', max)
  991. this.gl.drawArrays(this.gl.TRIANGLES, 0, 6)
  992. this.nodeBack.end()
  993. return this.swap()
  994. }
  995. Heights.prototype.multiply = function(value) {
  996. this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.heatmap.quad)
  997. this.gl.vertexAttribPointer(0, 4, this.gl.FLOAT, false, 0, 0)
  998. this.nodeFront.bind(0)
  999. this.nodeBack.use()
  1000. this.multiplyShader
  1001. .use()
  1002. .int('source', 0)
  1003. .float('value', value)
  1004. this.gl.drawArrays(this.gl.TRIANGLES, 0, 6)
  1005. this.nodeBack.end()
  1006. return this.swap()
  1007. }
  1008. Heights.prototype.blur = function() {
  1009. this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.heatmap.quad)
  1010. this.gl.vertexAttribPointer(0, 4, this.gl.FLOAT, false, 0, 0)
  1011. this.nodeFront.bind(0)
  1012. this.nodeBack.use()
  1013. this.blurShader
  1014. .use()
  1015. .int('source', 0)
  1016. .vec2('viewport', this.width, this.height)
  1017. this.gl.drawArrays(this.gl.TRIANGLES, 0, 6)
  1018. this.nodeBack.end()
  1019. return this.swap()
  1020. }
  1021. Heights.prototype.swap = function() {
  1022. let tmp = this.nodeFront
  1023. this.nodeFront = this.nodeBack
  1024. return (this.nodeBack = tmp)
  1025. }
  1026. Heights.prototype.addVertex = function(x, y, xs, ys, intensity) {
  1027. this.vertexBufferData[this.bufferIndex++] = x
  1028. this.vertexBufferData[this.bufferIndex++] = y
  1029. this.vertexBufferData[this.bufferIndex++] = xs
  1030. this.vertexBufferData[this.bufferIndex++] = ys
  1031. this.vertexBufferData[this.bufferIndex++] = intensity
  1032. this.vertexBufferData[this.bufferIndex++] = intensity
  1033. this.vertexBufferData[this.bufferIndex++] = intensity
  1034. return (this.vertexBufferData[this.bufferIndex++] = intensity)
  1035. }
  1036. Heights.prototype.addPoint = function(x, y, size, intensity) {
  1037. let s
  1038. if (size == null) {
  1039. size = 50
  1040. }
  1041. if (intensity == null) {
  1042. intensity = 0.2
  1043. }
  1044. if (this.pointCount >= this.maxPointCount - 1) {
  1045. this.update()
  1046. }
  1047. y = this.height - y
  1048. s = size / 2
  1049. this.addVertex(x, y, -s, -s, intensity)
  1050. this.addVertex(x, y, +s, -s, intensity)
  1051. this.addVertex(x, y, -s, +s, intensity)
  1052. this.addVertex(x, y, -s, +s, intensity)
  1053. this.addVertex(x, y, +s, -s, intensity)
  1054. this.addVertex(x, y, +s, +s, intensity)
  1055. return (this.pointCount += 1)
  1056. }
  1057. return Heights
  1058. })()
  1059. const WebGLHeatmap = (function() {
  1060. function WebGLHeatmap(_arg) {
  1061. let alphaEnd,
  1062. alphaRange,
  1063. alphaStart,
  1064. error,
  1065. getColorFun,
  1066. gradientTexture,
  1067. image,
  1068. intensityToAlpha,
  1069. output,
  1070. quad,
  1071. textureGradient,
  1072. _ref,
  1073. _ref1
  1074. ;(_ref = _arg != null ? _arg : {}),
  1075. (this.canvas = _ref.canvas),
  1076. (this.width = _ref.width),
  1077. (this.height = _ref.height),
  1078. (intensityToAlpha = _ref.intensityToAlpha),
  1079. (gradientTexture = _ref.gradientTexture),
  1080. (alphaRange = _ref.alphaRange)
  1081. if (!this.canvas) {
  1082. this.canvas = document.createElement('canvas')
  1083. }
  1084. try {
  1085. this.gl = this.canvas.getContext('experimental-webgl', {
  1086. depth: false,
  1087. antialias: false
  1088. })
  1089. if (this.gl === null) {
  1090. this.gl = this.canvas.getContext('webgl', {
  1091. depth: false,
  1092. antialias: false
  1093. })
  1094. if (this.gl === null) {
  1095. throw 'WebGL not supported'
  1096. }
  1097. }
  1098. } catch (_error) {
  1099. error = _error
  1100. throw 'WebGL not supported'
  1101. }
  1102. if (window.WebGLDebugUtils != null) {
  1103. // eslint-disable-next-line no-undef
  1104. this.gl = WebGLDebugUtils.makeDebugContext(this.gl, function(
  1105. err,
  1106. funcName,
  1107. args
  1108. ) {
  1109. // eslint-disable-next-line no-undef
  1110. throw WebGLDebugUtils.glEnumToString(err) +
  1111. ' was caused by call to: ' +
  1112. funcName
  1113. })
  1114. }
  1115. this.gl.enableVertexAttribArray(0)
  1116. this.gl.blendFunc(this.gl.ONE, this.gl.ONE)
  1117. if (gradientTexture) {
  1118. textureGradient = this.gradientTexture = new Texture(this.gl, {
  1119. channels: 'rgba'
  1120. })
  1121. .bind(0)
  1122. .setSize(2, 2)
  1123. .nearest()
  1124. .clampToEdge()
  1125. if (typeof gradientTexture === 'string') {
  1126. image = new Image()
  1127. image.onload = function() {
  1128. return textureGradient.bind().upload(image)
  1129. }
  1130. image.src = gradientTexture
  1131. } else {
  1132. if (gradientTexture.width > 0 && gradientTexture.height > 0) {
  1133. textureGradient.upload(gradientTexture)
  1134. } else {
  1135. gradientTexture.onload = function() {
  1136. return textureGradient.upload(gradientTexture)
  1137. }
  1138. }
  1139. }
  1140. getColorFun =
  1141. 'uniform sampler2D gradientTexture;\nvec3 getColor(float intensity){\n return texture2D(gradientTexture, vec2(intensity, 0.0)).rgb;\n}'
  1142. } else {
  1143. textureGradient = null
  1144. getColorFun =
  1145. 'vec3 getColor(float intensity){\n vec3 blue = vec3(0.0, 0.0, 1.0);\n vec3 cyan = vec3(0.0, 1.0, 1.0);\n vec3 green = vec3(0.0, 1.0, 0.0);\n vec3 yellow = vec3(1.0, 1.0, 0.0);\n vec3 red = vec3(1.0, 0.0, 0.0);\n\n vec3 color = (\n fade(-0.25, 0.25, intensity)*blue +\n fade(0.0, 0.5, intensity)*cyan +\n fade(0.25, 0.75, intensity)*green +\n fade(0.5, 1.0, intensity)*yellow +\n smoothstep(0.75, 1.0, intensity)*red\n );\n return color;\n}'
  1146. }
  1147. if (intensityToAlpha == null) {
  1148. intensityToAlpha = true
  1149. }
  1150. if (intensityToAlpha) {
  1151. ;(_ref1 = alphaRange != null ? alphaRange : [0, 1]),
  1152. (alphaStart = _ref1[0]),
  1153. (alphaEnd = _ref1[1])
  1154. output =
  1155. 'vec4 alphaFun(vec3 color, float intensity){\n float alpha = smoothstep(' +
  1156. alphaStart.toFixed(8) +
  1157. ', ' +
  1158. alphaEnd.toFixed(8) +
  1159. ', intensity);\n return vec4(color*alpha, alpha);\n}'
  1160. } else {
  1161. output =
  1162. 'vec4 alphaFun(vec3 color, float intensity){\n return vec4(color, 1.0);\n}'
  1163. }
  1164. this.shader = new Shader(this.gl, {
  1165. vertex: vertexShaderBlit,
  1166. fragment:
  1167. fragmentShaderBlit +
  1168. ('float linstep(float low, float high, float value){\n return clamp((value-low)/(high-low), 0.0, 1.0);\n}\n\nfloat fade(float low, float high, float value){\n float mid = (low+high)*0.5;\n float range = (high-low)*0.5;\n float x = 1.0 - clamp(abs(mid-value)/range, 0.0, 1.0);\n return smoothstep(0.0, 1.0, x);\n}\n\n' +
  1169. getColorFun +
  1170. '\n' +
  1171. output +
  1172. '\n\nvoid main(){\n float intensity = smoothstep(0.0, 1.0, texture2D(source, texcoord).r);\n vec3 color = getColor(intensity);\n gl_FragColor = alphaFun(color, intensity);\n}')
  1173. })
  1174. if (this.width == null) {
  1175. this.width = this.canvas.offsetWidth || 2
  1176. }
  1177. if (this.height == null) {
  1178. this.height = this.canvas.offsetHeight || 2
  1179. }
  1180. this.canvas.width = this.width
  1181. this.canvas.height = this.height
  1182. this.gl.viewport(0, 0, this.width, this.height)
  1183. this.quad = this.gl.createBuffer()
  1184. this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.quad)
  1185. quad = new Float32Array([
  1186. -1,
  1187. -1,
  1188. 0,
  1189. 1,
  1190. 1,
  1191. -1,
  1192. 0,
  1193. 1,
  1194. -1,
  1195. 1,
  1196. 0,
  1197. 1,
  1198. -1,
  1199. 1,
  1200. 0,
  1201. 1,
  1202. 1,
  1203. -1,
  1204. 0,
  1205. 1,
  1206. 1,
  1207. 1,
  1208. 0,
  1209. 1
  1210. ])
  1211. this.gl.bufferData(this.gl.ARRAY_BUFFER, quad, this.gl.STATIC_DRAW)
  1212. this.gl.bindBuffer(this.gl.ARRAY_BUFFER, null)
  1213. this.heights = new Heights(this, this.gl, this.width, this.height)
  1214. }
  1215. WebGLHeatmap.prototype.adjustSize = function() {
  1216. let canvasHeight, canvasWidth
  1217. canvasWidth = this.canvas.offsetWidth || 2
  1218. canvasHeight = this.canvas.offsetHeight || 2
  1219. if (this.width !== canvasWidth || this.height !== canvasHeight) {
  1220. this.gl.viewport(0, 0, canvasWidth, canvasHeight)
  1221. this.canvas.width = canvasWidth
  1222. this.canvas.height = canvasHeight
  1223. this.width = canvasWidth
  1224. this.height = canvasHeight
  1225. return this.heights.resize(this.width, this.height)
  1226. }
  1227. }
  1228. WebGLHeatmap.prototype.display = function() {
  1229. this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.quad)
  1230. this.gl.vertexAttribPointer(0, 4, this.gl.FLOAT, false, 0, 0)
  1231. this.heights.nodeFront.bind(0)
  1232. if (this.gradientTexture) {
  1233. this.gradientTexture.bind(1)
  1234. }
  1235. this.shader
  1236. .use()
  1237. .int('source', 0)
  1238. .int('gradientTexture', 1)
  1239. return this.gl.drawArrays(this.gl.TRIANGLES, 0, 6)
  1240. }
  1241. WebGLHeatmap.prototype.update = function() {
  1242. return this.heights.update()
  1243. }
  1244. WebGLHeatmap.prototype.clear = function() {
  1245. return this.heights.clear()
  1246. }
  1247. WebGLHeatmap.prototype.clamp = function(min, max) {
  1248. if (min == null) {
  1249. min = 0
  1250. }
  1251. if (max == null) {
  1252. max = 1
  1253. }
  1254. return this.heights.clamp(min, max)
  1255. }
  1256. WebGLHeatmap.prototype.multiply = function(value) {
  1257. if (value == null) {
  1258. value = 0.95
  1259. }
  1260. return this.heights.multiply(value)
  1261. }
  1262. WebGLHeatmap.prototype.blur = function() {
  1263. return this.heights.blur()
  1264. }
  1265. WebGLHeatmap.prototype.addPoint = function(x, y, size, intensity) {
  1266. return this.heights.addPoint(x, y, size, intensity)
  1267. }
  1268. WebGLHeatmap.prototype.addPoints = function(items) {
  1269. let item, _i, _len, _results
  1270. _results = []
  1271. for (_i = 0, _len = items.length; _i < _len; _i++) {
  1272. item = items[_i]
  1273. _results.push(this.addPoint(item.x, item.y, item.size, item.intensity))
  1274. }
  1275. return _results
  1276. }
  1277. return WebGLHeatmap
  1278. })()
  1279. export function createWebGLHeatmap(params) {
  1280. return new WebGLHeatmap(params)
  1281. }