@@ -129,25 +129,72 @@ 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
+ console . log ( anchors )
134
+ this . data . textContentList = this . data . textContentList . concat ( anchors . map ( anchor => {
135
+ let result = { }
136
+ result = {
137
+ text : anchor . text ,
138
+ subtext : anchor . subtext ,
139
+ box : anchor . box ,
140
+ centerX : anchor . centerX ,
141
+ centerY : anchor . centerY ,
142
+ }
143
+ if ( anchor . box ) {
144
+ let lt = anchor . box [ 0 ]
145
+ let lr = anchor . box [ 1 ]
146
+ let rb = anchor . box [ 2 ]
147
+ let lb = anchor . box [ 3 ]
148
+ let width = lr . x - lt . x
149
+ let height = lb . y - lt . y
150
+ let avgX = ( lt . x + lr . x + rb . x + lb . x ) / 4 ;
151
+ let avgY = ( lt . y + lr . y + rb . y + lb . y ) / 4 ;
152
+ anchor . centerX = avgX * this . data . faceImgWidth ;
153
+ anchor . centerY = avgY * this . data . faceImgHeight ;
154
+ result . origin = {
155
+ x : lt . x ,
156
+ y : lt . y ,
157
+ }
158
+ result . size = {
159
+ width : width ,
160
+ height : height ,
161
+ }
162
+ }
163
+ return result
164
+ } ) )
165
+
166
+ var wholeText = undefined
167
+ if ( this . data . textContentList . length != 0 ) {
168
+ wholeText = this . data . textContentList [ 0 ] . text
169
+ }
170
+
133
171
this . setData ( {
134
- textContentList : anchors . map ( anchor => ( {
135
- text : anchor . text
136
- } ) ) ,
172
+ textContentList : this . data . textContentList ,
173
+ wholeText : wholeText
137
174
} )
138
175
} )
139
176
session . on ( 'removeAnchors' , anchors => {
140
177
console . log ( "anchor remove" )
141
178
} )
142
179
180
+
181
+ //限制调用帧率
182
+ let fps = 30
183
+ let fpsInterval = 1000 / fps
184
+ let last = Date . now ( )
185
+
143
186
// 逐帧渲染
144
187
const onFrame = timestamp => {
145
- // let start = Date.now()
146
- const frame = session . getVKFrame ( canvas . width , canvas . height )
147
- if ( frame ) {
188
+ let now = Date . now ( )
189
+ const mill = now - last
190
+ // 经过了足够的时间
191
+ if ( mill > fpsInterval ) {
192
+ last = now - ( mill % fpsInterval ) ; //校正当前时间
193
+ const frame = session . getVKFrame ( canvas . width , canvas . height )
194
+ if ( frame ) {
148
195
this . render ( frame )
196
+ }
149
197
}
150
-
151
198
session . requestAnimationFrame ( onFrame )
152
199
}
153
200
session . requestAnimationFrame ( onFrame )
0 commit comments