Призрачный эффект от курсора на HTML5 Canvas
Замечательный призрачный эффект от курсора со стилизацией под помехи на HTML5 Canvas
Для начала посмотрите ДЕМО
Установка:
1#: Между тегами <head> и </head> на нужных страницах вашего сайта вставьте:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
<script src="/js/three.min2.js"></script> <script id="vertexShader" type="x-shader/x-vertex"> void main() { gl_Position = vec4( position, 1.0 ); } </script> <script id="fragmentShader" type="x-shader/x-fragment"> uniform vec2 u_resolution; uniform vec2 u_mouse; uniform float u_time; uniform sampler2D u_noise; uniform sampler2D u_buffer; uniform bool u_renderpass; const float blurMultiplier = 0.95; const float circleSize = .25; const float blurStrength = .98; const float threshold = .5; const float scale = 4.; #define _fract true #define PI 3.141592653589793 #define TAU 6.283185307179586 vec2 hash2(vec2 p) { vec2 o = texture2D( u_noise, (p+0.5)/256.0, -100.0 ).xy; return o; } vec3 hsb2rgb( in vec3 c ){ vec3 rgb = clamp(abs(mod(c.x*6.0+vec3(0.0,4.0,2.0), 6.0)-3.0)-1.0, 0.0, 1.0 ); rgb = rgb*rgb*(3.0-2.0*rgb); return c.z * mix( vec3(1.0), rgb, c.y); } vec3 domain(vec2 z){ return vec3(hsb2rgb(vec3(atan(z.y,z.x)/TAU,1.,1.))); } vec3 colour(vec2 z) { return domain(z); } #define pow2(x) (x * x) const int samples = 8; const float sigma = float(samples) * 0.25; float gaussian(vec2 i) { return 1.0 / (2.0 * PI * pow2(sigma)) * exp(-((pow2(i.x) + pow2(i.y)) / (2.0 * pow2(sigma)))); } vec3 hash33(vec3 p){ float n = sin(dot(p, vec3(7, 157, 113))); return fract(vec3(2097152, 262144, 32768)*n); } vec3 blur(sampler2D sp, vec2 uv, vec2 scale) { vec3 col = vec3(0.0); float accum = 0.0; float weight; vec2 offset; for (int x = -samples / 2; x < samples / 2; ++x) { for (int y = -samples / 2; y < samples / 2; ++y) { offset = vec2(x, y); weight = gaussian(offset); col += texture2D(sp, uv + scale * offset).rgb * weight; accum += weight; } } return col / accum; } void main() { vec2 uv = (gl_FragCoord.xy - 0.5 * u_resolution.xy) / u_resolution.y; uv *= scale; vec2 mouse = u_mouse * scale; vec2 ps = vec2(1.0) / u_resolution.xy; vec2 sample = gl_FragCoord.xy / u_resolution.xy; vec2 o = mouse*.2+vec2(.65, .5); float d = .98; sample = d * (sample - o); sample += o; sample += vec2(sin((u_time+uv.y * .5)*10.)*.001, -.00); vec3 fragcolour; vec4 tex; if(u_renderpass) { tex = vec4(blur(u_buffer, sample, ps*blurStrength) * blurMultiplier, 1.); float df = length(mouse - uv); fragcolour = vec3( smoothstep( circleSize, 0., df ) ); } else { tex = texture2D(u_buffer, sample, 2.) * .98; tex = vec4( smoothstep(0.0, threshold - fwidth(tex.x), tex.x), smoothstep(0.2, threshold - fwidth(tex.y) + .2, tex.y), smoothstep(-0.05, threshold - fwidth(tex.z) - .2, tex.z), 1.); vec3 n = hash33(vec3(uv, u_time*.1)); tex.rgb += n * .2 - .1; // tex = vec4( // smoothstep(0.0, threshold - fwidth(tex.x), tex.x), // smoothstep(0.2, threshold - fwidth(tex.x) + .2, tex.x), // smoothstep(-0.05, threshold - fwidth(tex.x) - .2, tex.x), // 1.); } // vec4 tex = texture2D(u_buffer, sample, 2.) * .98; gl_FragColor = vec4(fragcolour,1.0); gl_FragColor += tex; } </script> |
2#: В самый низ вашего CSS поместите:
1 2 3 4 |
#ghost { position: fixed; touch-action: none; } |
3#: Между тегами body и /body поместите:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
<div id="ghost" touch-action="none"></div> <script src="/js/ccapture.js"></script> <script> /* Most of the stuff in here is just bootstrapping. Essentially it's just setting ThreeJS up so that it renders a flat surface upon which to draw the shader. The only thing to see here really is the uniforms sent to the shader. Apart from that all of the magic happens in the HTML view under the fragment shader. */ var container = void 0; var camera = void 0,scene = void 0,renderer = void 0; var uniforms = void 0; var divisor = 1 / 10; var newmouse = { x: 0, y: 0 }; var loader = new THREE.TextureLoader(); var texture = void 0,rtTexture = void 0,rtTexture2 = void 0; loader.setCrossOrigin("anonymous"); loader.load( '/img/noise.png', function do_something_with_texture(tex) { texture = tex; texture.wrapS = THREE.RepeatWrapping; texture.wrapT = THREE.RepeatWrapping; texture.minFilter = THREE.LinearFilter; init(); animate(); }); function init() { container = document.getElementById('ghost'); camera = new THREE.Camera(); camera.position.z = 1; scene = new THREE.Scene(); var geometry = new THREE.PlaneBufferGeometry(2, 2); rtTexture = new THREE.WebGLRenderTarget(window.innerWidth * .2, window.innerHeight * .2); rtTexture2 = new THREE.WebGLRenderTarget(window.innerWidth * .2, window.innerHeight * .2); uniforms = { u_time: { type: "f", value: 1.0 }, u_resolution: { type: "v2", value: new THREE.Vector2() }, u_noise: { type: "t", value: texture }, u_buffer: { type: "t", value: rtTexture.texture }, u_mouse: { type: "v2", value: new THREE.Vector2() }, u_renderpass: { type: 'b', value: false } }; var material = new THREE.ShaderMaterial({ uniforms: uniforms, vertexShader: document.getElementById('vertexShader').textContent, fragmentShader: document.getElementById('fragmentShader').textContent }); material.extensions.derivatives = true; var mesh = new THREE.Mesh(geometry, material); scene.add(mesh); renderer = new THREE.WebGLRenderer(); renderer.setPixelRatio(window.devicePixelRatio); container.appendChild(renderer.domElement); onWindowResize(); window.addEventListener('resize', onWindowResize, false); document.addEventListener('pointermove', function (e) { var ratio = window.innerHeight / window.innerWidth; newmouse.x = (e.pageX - window.innerWidth / 2) / window.innerWidth / ratio; newmouse.y = (e.pageY - window.innerHeight / 2) / window.innerHeight * -1; e.preventDefault(); }); } function onWindowResize(event) { renderer.setSize(window.innerWidth, window.innerHeight); uniforms.u_resolution.value.x = renderer.domElement.width; uniforms.u_resolution.value.y = renderer.domElement.height; rtTexture = new THREE.WebGLRenderTarget(window.innerWidth * .2, window.innerHeight * .2); rtTexture2 = new THREE.WebGLRenderTarget(window.innerWidth * .2, window.innerHeight * .2); } function animate(delta) { requestAnimationFrame(animate); render(delta); } var capturer = new CCapture({ verbose: true, framerate: 60, // motionBlurFrames: 4, quality: 90, format: 'webm', workersPath: 'js/' }); var capturing = false; isCapturing = function isCapturing(val) { if (val === false && window.capturing === true) { capturer.stop(); capturer.save(); } else if (val === true && window.capturing === false) { capturer.start(); } capturing = val; }; toggleCapture = function toggleCapture() { isCapturing(!capturing); }; window.addEventListener('keyup', function (e) {if (e.keyCode == 68) toggleCapture();}); var then = 0; function renderTexture(delta) { // let ov = uniforms.u_buff.value; var odims = uniforms.u_resolution.value.clone(); uniforms.u_resolution.value.x = window.innerWidth * .2; uniforms.u_resolution.value.y = window.innerHeight * .2; uniforms.u_buffer.value = rtTexture2.texture; uniforms.u_renderpass.value = true; // rtTexture = rtTexture2; // rtTexture2 = buffer; window.rtTexture = rtTexture; renderer.setRenderTarget(rtTexture); renderer.render(scene, camera, rtTexture, true); var buffer = rtTexture; rtTexture = rtTexture2; rtTexture2 = buffer; // uniforms.u_buff.value = ov; uniforms.u_buffer.value = rtTexture.texture; uniforms.u_resolution.value = odims; uniforms.u_renderpass.value = false; } function render(delta) { uniforms.u_mouse.value.x += (newmouse.x - uniforms.u_mouse.value.x) * divisor; uniforms.u_mouse.value.y += (newmouse.y - uniforms.u_mouse.value.y) * divisor; uniforms.u_time.value = delta * 0.0005; renderer.render(scene, camera); renderTexture(); if (capturing) { capturer.capture(renderer.domElement); } } </script> |
Осталось лишь залить из прикреплённого архива два JS файла в папку js и одну картинку в папку img
Тестовый коммент
Ещё один
Ещё
не работает, как и всё остальное впринципе
Что значит всё остальное? Этот материал проверен и работает. Смотрите ДЕМО