@@ -5,11 +5,20 @@ import getShader from './getShader';
55import loadProgram from './loadProgram' ;
66import createImageGrid from './createImageGrid' ;
77import TarToMp4 from './tarToMp4' ;
8+ import * as globalStyles from '../../globalStyles' ;
9+ import hexToRGB from '../../lib/hexToRGB' ;
810
911class Preview {
1012 constructor ( boundingRect , exportCallback ) {
1113 this . boundingRect = boundingRect ;
1214 this . exportCallback = exportCallback ;
15+ this . overlayColor = hexToRGB ( globalStyles . action ) ;
16+
17+ // normalize color
18+ this . overlayColor . r = this . overlayColor . r / 255.0 ;
19+ this . overlayColor . g = this . overlayColor . g / 255.0 ;
20+ this . overlayColor . b = this . overlayColor . b / 255.0 ;
21+ this . showOverlay = false ;
1322
1423 this . canvas = document . getElementById ( 'cinemagraphcanvas' ) ;
1524 this . gl = this . canvas . getContext ( 'webgl' ) ;
@@ -64,7 +73,15 @@ class Preview {
6473 let gl = this . gl ;
6574 try {
6675 let vertexshader = getShader ( gl , cinemagraphVertexShader , 'vertex' ) ;
67- let fragmentshader = getShader ( gl , cinemagraphFragShader , 'frag' ) ;
76+ let fragmentshader = getShader (
77+ gl ,
78+ cinemagraphFragShader (
79+ this . overlayColor . r . toFixed ( 5 ) . toString ( ) ,
80+ this . overlayColor . g . toFixed ( 5 ) . toString ( ) ,
81+ this . overlayColor . b . toFixed ( 5 ) . toString ( ) ,
82+ ) ,
83+ 'frag' ,
84+ ) ;
6885
6986 this . pictureprogram = loadProgram ( gl , vertexshader , fragmentshader ) ;
7087 gl . useProgram ( this . pictureprogram ) ;
@@ -98,9 +115,15 @@ class Preview {
98115 'u_image1' ,
99116 ) ;
100117
118+ this . pictureprogram . show_overlay = gl . getUniformLocation (
119+ this . pictureprogram ,
120+ 'show_overlay' ,
121+ ) ;
122+
101123 // Set the texture to use.
102124 gl . uniform1i ( this . pictureprogram . u_image0 , 0 ) ;
103125 gl . uniform1i ( this . pictureprogram . u_image1 , 1 ) ;
126+ gl . uniform1i ( this . pictureprogram . show_overlay , 2 ) ;
104127 } catch ( e ) {
105128 console . log ( 'ERROR MAKING SHADERS: ' , e ) ;
106129 return ;
@@ -135,6 +158,10 @@ class Preview {
135158 this . start ( ) ;
136159 } ;
137160
161+ setShowOverlay = newOverlay => {
162+ this . showOverlay = newOverlay ;
163+ } ;
164+
138165 update = ( newPoint , brushSize , brushBlur , brushTool ) => {
139166 const normalizedPoint = this . normalizedPoint (
140167 newPoint [ 0 ] ,
@@ -144,30 +171,50 @@ class Preview {
144171 ) ;
145172
146173 // normalize brush size based off of actual image width
147- let normalizedBrushSize =
148- brushSize * this . videoWidth / this . boundingRect . width ;
174+ let normalizedBrushSize = Math . ceil (
175+ brushSize * this . videoWidth / this . boundingRect . width ,
176+ ) ;
177+
178+ const minX = Math . floor (
179+ Math . max ( normalizedPoint . x - normalizedBrushSize , 0 ) ,
180+ ) ;
181+ const maxX = Math . min (
182+ normalizedPoint . x + normalizedBrushSize ,
183+ this . videoWidth ,
184+ ) ;
185+ const minY = Math . floor (
186+ Math . max ( normalizedPoint . y - normalizedBrushSize , 0 ) ,
187+ ) ;
188+ const maxY = Math . min (
189+ normalizedPoint . y + normalizedBrushSize ,
190+ this . videoHeight ,
191+ ) ;
149192
150193 let blur = 21 - 2 * brushBlur ;
151194
152- let l = this . brushedImage . data . length / 4 ;
153- for ( let i = 0 ; i < l ; i ++ ) {
154- const x = i % this . videoWidth ;
155- const y = Math . min ( i / this . videoWidth ) ;
156- let dist = Math . sqrt (
157- Math . pow ( normalizedPoint . x - x , 2 ) + Math . pow ( normalizedPoint . y - y , 2 ) ,
158- ) ;
159- if ( dist < normalizedBrushSize ) {
160- const normalizedDistance =
161- Math . pow ( dist / normalizedBrushSize , blur ) * 255.0 ;
162-
163- const opacity =
164- brushTool == 'eraser'
165- ? Math . min ( this . brushedImage . data [ i * 4 + 3 ] , normalizedDistance )
166- : Math . max (
167- this . brushedImage . data [ i * 4 + 3 ] ,
168- 255.0 - normalizedDistance ,
169- ) ;
170- this . brushedImage . data [ i * 4 + 3 ] = opacity ;
195+ // only iterate over the coordinates within
196+ // the square of brush size
197+ for ( let y = 0 ; y < maxY ; y ++ ) {
198+ for ( let x = 0 ; x < maxX ; x ++ ) {
199+ const i = this . videoWidth * y + x ;
200+ // console.log("I: ", i);
201+ let dist = Math . sqrt (
202+ Math . pow ( normalizedPoint . x - x , 2 ) +
203+ Math . pow ( normalizedPoint . y - y , 2 ) ,
204+ ) ;
205+ if ( dist < normalizedBrushSize ) {
206+ const normalizedDistance =
207+ Math . pow ( dist / normalizedBrushSize , blur ) * 255.0 ;
208+
209+ const opacity =
210+ brushTool == 'eraser'
211+ ? Math . min ( this . brushedImage . data [ i * 4 + 3 ] , normalizedDistance )
212+ : Math . max (
213+ this . brushedImage . data [ i * 4 + 3 ] ,
214+ 255.0 - normalizedDistance ,
215+ ) ;
216+ this . brushedImage . data [ i * 4 + 3 ] = opacity ;
217+ }
171218 }
172219 }
173220 } ;
@@ -244,6 +291,11 @@ class Preview {
244291
245292 gl . enableVertexAttribArray ( this . texCoordLocation ) ;
246293
294+ gl . uniform1i (
295+ gl . getUniformLocation ( this . pictureprogram , 'show_overlay' ) ,
296+ this . showOverlay ,
297+ ) ;
298+
247299 gl . drawArrays ( gl . TRIANGLES , 0 , resolution * resolution * 2 * 3 ) ;
248300 } ;
249301
0 commit comments