Comments (1)
`//@flow
import {Shaders, Node, GLSL, connectSize, LinearCopy, Uniform} from 'gl-react';
import React, {Component} from 'react';
const shaders = Shaders.create({
contain: {
vert: GLSL'
attribute vec2 _p;
varying vec2 uv;
uniform float tR;
uniform vec2 res;
float r;
void main() {
r = res.x / res.y;
gl_Position = vec4(_p,0.0,1.0);
uv = .5+0.5*_pvec2(max(r/tR,1.),max(tR/r,1.));
}
',
frag: GLSL'
precision highp float;
varying vec2 uv;
uniform sampler2D t;
void main () {
gl_FragColor =
step(0.0, uv.x) *
step(0.0, uv.y) *
step(uv.x, 1.0) *
step(uv.y, 1.0) *
texture2D(t, uv);
}
',
},
free: {
vert: GLSL'
attribute vec2 _p;
varying vec2 uv;
uniform float zoom;
uniform vec2 center;
uniform float tR;
uniform vec2 res;
float r;
vec2 invert (vec2 p) {
return vec2(p.x, 1.0-p.y);
}
void main() {
r = res.x / res.y;
gl_Position = vec4(_p,0.0,1.0);
// crop with zoom & center in a cover mode. preserving image ratio
float maxR = max(r, tR);
vec2 zoomedCanvasSize = vec2(
(r / maxR) * zoom,
(tR / maxR) * zoom
);
vec4 crop = vec4(
center.x - zoomedCanvasSize.x / 2.,
center.y - zoomedCanvasSize.y / 2.,
zoomedCanvasSize.x,
zoomedCanvasSize.y
);
// apply the crop rectangle
uv = invert(invert(.5+.5_p) * crop.zw + crop.xy);
}
',
frag: GLSL'
precision highp float;
varying vec2 uv;
uniform sampler2D t;
void main () {
gl_FragColor =
step(0.0, uv.x) *
step(0.0, uv.y) *
step(uv.x, 1.0) *
step(uv.y, 1.0) *
texture2D(t, uv);
}
',
},
cover: {
// NB the cover vertex code can probably be simplified. good enough for now.
vert: GLSL'
attribute vec2 _p;
varying vec2 uv;
uniform float zoom;
uniform vec2 center;
uniform float tR;
uniform vec2 res;
float r;
vec2 invert (vec2 p) {
return vec2(p.x, 1.0-p.y);
}
void main() {
r = res.x / res.y;
gl_Position = vec4(_p,0.0,1.0);
// crop with zoom & center in a cover mode. preserving image ratio
float maxR = max(r, tR);
vec2 zoomedCanvasSize = vec2(
(r / maxR) * zoom,
(tR / maxR) * zoom
);
vec4 crop = vec4(
center.x - zoomedCanvasSize.x / 2.,
center.y - zoomedCanvasSize.y / 2.,
zoomedCanvasSize.x,
zoomedCanvasSize.y
);
// clamp to not escape the edges
float w = crop[2], h = crop[3];
float ratio = w / h;
if (w > 1.) {
w = 1.;
h = w / ratio;
}
if (h > 1.) {
h = 1.;
w = h * ratio;
}
crop = vec4(
max(0., min(crop.x, 1.-w)),
max(0., min(crop.y, 1.-h)),
w,
h
);
// apply the crop rectangle
uv = invert(invert(.5+.5*_p) * crop.zw + crop.xy);
}
',
frag: GLSL'
precision highp float;
varying vec2 uv;
uniform sampler2D t;
void main () {
gl_FragColor = texture2D(t, uv);
}
',
},
});
export default class GLImage extends Component {
// props:
// | {
// source: any,
// resizeMode: "cover" | "free",
// center?: [number, number],
// zoom?: number
// }
// | {
// source: any,
// resizeMode: "contain"
// }
// | {
// source: any,
// resizeMode: "stretch"
// };
static defaultProps = {
resizeMode: 'cover',
};
render() {
const {props} = this;
const {source, resizeMode, ...rest} = props;
if (resizeMode === 'cover' || resizeMode === 'free') {
const center = props.center || [0.5, 0.5];
const zoom = props.zoom || 1;
delete rest.center;
delete rest.zoom;
return (
<Node
{...rest}
shader={resizeMode === 'cover' ? shaders.cover : shaders.free}
uniforms={{
t: source,
tR: Uniform.textureSizeRatio(source),
res: Uniform.Resolution,
center,
zoom,
}}
/>
);
}
if (resizeMode === 'contain') {
return (
<Node
{...rest}
shader={shaders.contain}
uniforms={{
t: source,
tR: Uniform.textureSizeRatio(source),
res: Uniform.Resolution,
}}
/>
);
}
// fallback on stretch, most basic thing
return <LinearCopy {...rest}>{source}</LinearCopy>;
}
}
`
from gl-react.
Related Issues (20)
- [Feature Request] Uint8Array as a Texture HOT 6
- Possible Bug: Image data flipped when captured
- TypeError: undefined is not a function (near '...GL.createComponent...') HOT 1
- image disappeared after refocus the screen
- gl-react-native App crashed with Surface with style prop HOT 5
- Update Peer Dependency to gl-react-5?
- GL is currently not available (have you enabled remote debbuging) shows up on Android HOT 1
- Camera preview dimensions ignored? - distorted on iOS HOT 2
- Question About Texts on Images in react-native HOT 1
- [iOS] app crash calling "capture": NSInvalidArgumentException HOT 2
- How to load spine animation json file in react-native HOT 1
- Specify texture format
- GL.Node does not support video surfaces HOT 2
- Failed to execute 'shaderSource' on 'WebGL2RenderingContext': parameter 1 is not of type 'WebGLShader' HOT 4
- Bad quality camera on Android devices HOT 5
- Image from local storage are not reading , surface view coming black.
- headless example HOT 1
- How to use gl-react with next.js ? Is there any documentation about this ? HOT 1
- Runtime error: `Unable to resolve module expo-modules-core`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gl-react.