])
/** Compile */
- static {
+ static async init() {
this.#gl = new OffscreenCanvas(this.#WORKLOAD, this.#WORKLOAD).getContext('webgl2')
if (this.#gl == null) throw new Error('WebGL 2 is required')
this.#gl.clearColor(0, 0, 0, 1)
let nonce = null
const work = new Uint8Array(8)
while (nonce == null) {
- this.draw(work)
- const found = await this.checkQueryResult()
+ this.#draw(work)
+ const found = await this.#checkQueryResult()
if (found) {
- nonce = this.readResult(work)
+ nonce = this.#readResult(work)
}
}
return nonce
}
- static draw (work: Uint8Array): void {
+ static #draw (work: Uint8Array): void {
if (this.#gl == null || this.#query == null) throw new Error('WebGL 2 is required to draw and query pixels')
if (this.#workBuffer == null) throw new Error('Work buffer is required to draw')
this.#gl.clear(this.#gl.COLOR_BUFFER_BIT)
this.#gl.endQuery(this.#gl.ANY_SAMPLES_PASSED_CONSERVATIVE)
}
- static async checkQueryResult (): Promise<boolean> {
+ static async #checkQueryResult (): Promise<boolean> {
if (this.#gl == null || this.#query == null) throw new Error('WebGL 2 is required to check query results')
if (this.#gl.getQueryParameter(this.#query, this.#gl.QUERY_RESULT_AVAILABLE)) {
return this.#gl.getQueryParameter(this.#query, this.#gl.QUERY_RESULT)
return new Promise((resolve, reject): void => {
try {
requestAnimationFrame(async (): Promise<void> => {
- const result = await NanoPowGl.checkQueryResult()
+ const result = await NanoPowGl.#checkQueryResult()
resolve(result)
})
} catch (err) {
* @param work - Buffer with the original random nonce value
* @returns Nonce as an 8-byte (16-char) hexadecimal string
*/
- static readResult (work: Uint8Array): string {
+ static #readResult (work: Uint8Array): string {
if (this.#gl == null) throw new Error('WebGL 2 is required to read pixels')
this.#gl.readPixels(0, 0, this.#gl.drawingBufferWidth, this.#gl.drawingBufferHeight, this.#gl.RGBA, this.#gl.UNSIGNED_BYTE, this.#pixels)
for (let i = 0; i < this.#pixels.length; i += 4) {