-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tong Shi
committed
Jul 20, 2024
1 parent
ac47989
commit 5ab981d
Showing
15 changed files
with
829 additions
and
203 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Document</title> | ||
<script src="../../dist/ezpsy.js"></script> | ||
</head> | ||
<body> | ||
<div id="sinGrat"></div> | ||
</body> | ||
<script> | ||
const average = (arr) => { | ||
let num = 0 | ||
arr.forEach(item => { | ||
num += item | ||
}) | ||
return num / arr.length | ||
} | ||
(async () => { | ||
let ez = await ezpsy.init({ | ||
el: document.getElementById('sinGrat'), | ||
}); | ||
const list = [] | ||
let a = new ezpsy.wasmSinGrating({ | ||
shape: { | ||
x: window.innerWidth / 2, | ||
y: window.innerHeight / 2, | ||
r: 1280, | ||
pixelsPerDegree: 57, | ||
spatialFrequency: 2, | ||
angle: 0, | ||
contrast: 1, | ||
phase: 0, | ||
level: 0.5, | ||
gamma: 1 | ||
}, | ||
isNoise: true | ||
}) | ||
for(let i = 0; i < 1; i++) { | ||
const t0 = performance.now() | ||
await a.pre_draw() | ||
const t1 = performance.now() | ||
console.log(t1 - t0) | ||
list.push(t1 - t0) | ||
} | ||
console.log(list) | ||
console.log("AVERAGE", average(list)) | ||
})() | ||
|
||
</script> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import { Shape,Opts,Style,nameStyle } from '../DataType/dataType' | ||
import { Elements } from '../Element'; | ||
// import * as SG from '../../static/pkg/singrat' | ||
import * as TIME from '../Time/time' | ||
import { getWasm } from "../setWasm" | ||
import * as SG from '../setWasm' | ||
import * as SGW from "../initWasm" | ||
|
||
interface GratingShape extends Shape{ | ||
x: number | ||
y: number | ||
r: number | ||
pixelsPerDegree?: number | ||
spatialFrequency?: number | ||
timeFrequency?: number | ||
angle?: number | ||
contrast?: number | ||
phase?: number | ||
level?: number | ||
gamma?: number | ||
} | ||
|
||
export interface GratingOpts extends Opts{ | ||
shape: GratingShape | ||
style?: Style | ||
isNoise?: boolean | ||
time?: number | ||
} | ||
|
||
let nameId = 0; | ||
|
||
export class wasmSinGrating extends Elements{ | ||
readonly name?: nameStyle = { | ||
name: "singrating" + nameId.toString(), | ||
graphicId: nameId | ||
} | ||
param: Uint8Array; | ||
width: number; | ||
sinGrat: ImageData; //光栅图片数据 | ||
imgDataList: Array<ImageData>; //用于储存参与动画的图片 | ||
isNoise: boolean; | ||
fps: number; | ||
timeFrequency: number | ||
constructor(opts: GratingOpts){ | ||
super(); | ||
this.shape = opts.shape; | ||
let sh = this.shape; | ||
this.width = 2*(sh.r/2+sh.r)+1; | ||
this.sinGrat = new ImageData(this.width,this.width); | ||
this.imgDataList = new Array<ImageData>(); | ||
this.isNoise = opts.isNoise; | ||
this.shape.pixelsPerDegree = !this.shape.pixelsPerDegree ? 57 : this.shape.pixelsPerDegree | ||
this.shape.spatialFrequency = !this.shape.spatialFrequency ? 2 : this.shape.spatialFrequency | ||
this.shape.angle = !this.shape.angle ? 0 : this.shape.angle | ||
this.shape.contrast = !this.shape.contrast ? 1 : this.shape.contrast | ||
this.shape.phase = !this.shape.phase ? 0 : this.shape.phase | ||
this.shape.level = !this.shape.level ? 0.5 : this.shape.level | ||
this.shape.gamma = !this.shape.gamma ? 1 : this.shape.gamma | ||
const timeFrequency = opts.shape.timeFrequency || 0 | ||
this.timeFrequency = timeFrequency | ||
this.fps = 60 | ||
|
||
nameId++; | ||
} | ||
async pre_draw() { | ||
const timeFrequency = this.timeFrequency | ||
let sh = this.shape; | ||
let param = [] | ||
if(!timeFrequency) { | ||
// const t0 = performance.now() | ||
if(this.isNoise) { | ||
param = SGW.generate(sh.r,sh.pixelsPerDegree,sh.spatialFrequency,sh.angle,sh.contrast,sh.phase,sh.level,sh.gamma); | ||
} | ||
else | ||
param = SG.pre_singrat(sh.r,sh.pixelsPerDegree,sh.spatialFrequency,sh.angle,sh.contrast,sh.phase,sh.gamma); | ||
for (let i = 0, j = 0; i < this.sinGrat.data.length; i += 4, j++) { | ||
this.sinGrat.data[i + 0] = param[j]; | ||
this.sinGrat.data[i + 1] = param[j]; | ||
this.sinGrat.data[i + 2] = param[j]; | ||
this.sinGrat.data[i + 3] = 255; | ||
} | ||
// const t1 = performance.now() | ||
// console.log("TIME", t1 - t0) | ||
} else { | ||
let interval = 2*Math.PI*timeFrequency/this.fps; | ||
let sh = this.shape; | ||
const array = new Array(Math.ceil(this.fps)).fill(0); | ||
if(this.isNoise) | ||
{ | ||
await Promise.all(array.map(async (item, index) => { | ||
let param = SG.pre_noise_singrat(sh.r,sh.pixelsPerDegree,sh.spatialFrequency,sh.angle,sh.contrast,sh.phase+index*interval,sh.level,sh.gamma); | ||
const img = new Array() | ||
for (let i = 0, j = 0; i < this.sinGrat.data.length; i += 4, j++) { | ||
img[i + 0] = param[j]; | ||
img[i + 1] = param[j]; | ||
img[i + 2] = param[j]; | ||
img[i + 3] = 255; | ||
} | ||
let imgData = new ImageData(new Uint8ClampedArray(img),this.width,this.width) | ||
this.imgDataList[index] = imgData | ||
})) | ||
} | ||
else{ | ||
await Promise.all(array.map(async (item, index) => { | ||
const wasm = await getWasm() | ||
let param = SG.pre_singrat(sh.r,sh.pixelsPerDegree,sh.spatialFrequency,sh.angle,sh.contrast,sh.phase+index*interval,sh.gamma); | ||
const img = new Array() | ||
for (let i = 0, j = 0; i < this.sinGrat.data.length; i += 4, j++) { | ||
img[i + 0] = param[j]; | ||
img[i + 1] = param[j]; | ||
img[i + 2] = param[j]; | ||
img[i + 3] = 255; | ||
} | ||
let imgData = new ImageData(new Uint8ClampedArray(img),this.width,this.width) | ||
this.imgDataList[index] = imgData | ||
})) | ||
} | ||
} | ||
} | ||
async draw(time: number = 1000){ | ||
let sh = this.shape; | ||
// const t0 = performance.now(); | ||
if(!this.timeFrequency) { | ||
this.ctx.putImageData(this.sinGrat,sh.x-1.5*sh.r,sh.y-1.5*sh.r) | ||
} else { | ||
const fps = this.fps | ||
let fpsNum = Math.floor(time / 1000 * fps); | ||
let index = 0; | ||
let sh = this.shape; | ||
let that = this; | ||
console.log(that.imgDataList[0]); | ||
await (async ()=>{ | ||
for(let i = 0;i < fpsNum;i++) { | ||
index = i % fps; | ||
that.ctx.putImageData(that.imgDataList[index],sh.x-1.5*sh.r,sh.y-1.5*sh.r); | ||
await TIME.delay_frame(1); | ||
that.remove(); | ||
} | ||
})(); | ||
} | ||
// const t1 = performance.now() | ||
// console.log("TIME DELAY", t1 - t0) | ||
} | ||
play(time: number = 1000) { | ||
const fps = this.fps | ||
let fpsNum = Math.floor(time / 1000 * fps); | ||
let index = 0; | ||
let sh = this.shape; | ||
let that = this; | ||
(async ()=>{ | ||
for(let i = 0;i < fpsNum;i++) | ||
{ | ||
index = i % fps; | ||
that.ctx.putImageData(that.imgDataList[index],sh.x-1.5*sh.r,sh.y-1.5*sh.r); | ||
await TIME.delay_frame(1); | ||
that.remove(); | ||
} | ||
})() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.