@@ -129,25 +129,69 @@ export default function getBehavior() {
129
129
console . log ( "anchor add" )
130
130
} )
131
131
session . on ( 'updateAnchors' , anchors => {
132
- // 手动传入图像的时候用dom画点和框就行
132
+ this . data . textContentList = [ ]
133
+
134
+ // 摄像头实时检测人脸的时候 updateAnchors 会在每帧触发,所以性能要求更高,用 gl 画
135
+ this . data . textContentList = this . data . textContentList . concat ( anchors . map ( anchor => {
136
+ let lt = anchor . box [ 0 ]
137
+ let lr = anchor . box [ 1 ]
138
+ let rb = anchor . box [ 2 ]
139
+ let lb = anchor . box [ 3 ]
140
+ let width = lr . x - lt . x
141
+ let height = lb . y - lt . y
142
+ let avgX = ( lt . x + lr . x + rb . x + lb . x ) / 4 ;
143
+ let avgY = ( lt . y + lr . y + rb . y + lb . y ) / 4 ;
144
+ anchor . centerX = avgX * this . data . faceImgWidth ;
145
+ anchor . centerY = avgY * this . data . faceImgHeight ;
146
+ return {
147
+ text : anchor . text ,
148
+ subtext : anchor . subtext ,
149
+ box : anchor . box ,
150
+ centerX : anchor . centerX ,
151
+ centerY : anchor . centerY ,
152
+ origin : {
153
+ x : lt . x ,
154
+ y : lt . y ,
155
+ } ,
156
+ size : {
157
+ width : width ,
158
+ height : height ,
159
+ }
160
+ } ;
161
+ } ) )
162
+
163
+ var wholeText = undefined
164
+ if ( this . data . textContentList . length != 0 ) {
165
+ wholeText = this . data . textContentList [ 0 ] . text
166
+ }
167
+
133
168
this . setData ( {
134
- textContentList : anchors . map ( anchor => ( {
135
- text : anchor . text
136
- } ) ) ,
169
+ textContentList : this . data . textContentList ,
170
+ wholeText : wholeText
137
171
} )
138
172
} )
139
173
session . on ( 'removeAnchors' , anchors => {
140
174
console . log ( "anchor remove" )
141
175
} )
142
176
177
+
178
+ //限制调用帧率
179
+ let fps = 30
180
+ let fpsInterval = 1000 / fps
181
+ let last = Date . now ( )
182
+
143
183
// 逐帧渲染
144
184
const onFrame = timestamp => {
145
- // let start = Date.now()
146
- const frame = session . getVKFrame ( canvas . width , canvas . height )
147
- if ( frame ) {
185
+ let now = Date . now ( )
186
+ const mill = now - last
187
+ // 经过了足够的时间
188
+ if ( mill > fpsInterval ) {
189
+ last = now - ( mill % fpsInterval ) ; //校正当前时间
190
+ const frame = session . getVKFrame ( canvas . width , canvas . height )
191
+ if ( frame ) {
148
192
this . render ( frame )
193
+ }
149
194
}
150
-
151
195
session . requestAnimationFrame ( onFrame )
152
196
}
153
197
session . requestAnimationFrame ( onFrame )
0 commit comments