こんにちは。
普段はデザイン担当で簡単なHTML,CSSしか触ったことがないのですが、
急遽私の方でJSを使った動きのあるテキストをHPの見出しとして実装することとなりました。
下記のURLのようなマウスをhverするとぐにゃっとなる動きを使用したいです。
下記サイトのコードをまるまるもってきて、実装するところまでは出来ました。
https://goworkship.com/magazine/text-effect-typography/#23
これから背景を削除したり、テキストの大きさや色、位置を変えたりしたいのですが、
どの部分を変更すればよいかわからず困っています。
なんとか調べ、文字はjsファイルのテキスト部分を変えると変わることが分かったのですが。。。
下記、私が記載しているソースコードです。
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="style.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.19.0/TweenMax.min.js"></script> </head> <body> <script id="fragmentShader" type="x-shader/x-fragment"> precision mediump float; uniform float time; uniform float mouseX; uniform float mouseY; uniform sampler2D texture; varying vec2 vUv; void main() { float d = -distance(vec2(mouseX,mouseY), gl_FragCoord.xy); float r = dot(gl_FragCoord.xy, vec2(0.005,0.005))/d; vec2 tex = vec2(vUv.x + r, vUv.y + r); gl_FragColor = vec4(texture2D(texture, tex).rgb, 1.0); } </script> <script id="vertexShader" type="x-shader/x-vertex"> precision mediump float; varying vec2 vUv; uniform float time; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); } </script> <div class="stage"></div> <canvas class="noise-canvas"></canvas> <script src="test.js"></script> </body> </html>
CSS
.noise-canvas{ position: absolute; top: 0; left: 0; bottom: 0; right: 0; width: 100%; height: 100%; opacity: 0.3; z-index: 2; }
JS
// Copyright (c) 2021 by Corentin (https://codepen.io/corentinfardeau/pen/VmZBpv) // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict' console.clear(); const APP_CONFIG = { debug: false, gridDebugSize: { x: 10, y: 10 }, fontSize: 600, axisHelperSize: 10 } class App { constructor(){ _.bindAll(this, 'animate', 'onResize', 'onMouseMove'); this.time = 0; this.planeHeight = 50; this.ratio = window.innerWidth / window.innerHeight; this.planeWidth = this.planeHeight*this.ratio; //SET-UP CAMERA this.cameraOpts = { aspect: window.innerWidth / window.innerHeight, near: 0.1, far : 10000, z: this.planeHeight } let fov = 2 * Math.atan( this.planeHeight / ( 2 * this.cameraOpts.z ) ) * ( 180 / Math.PI ); this.camera = new THREE.PerspectiveCamera(fov, this.cameraOpts.aspect, this.cameraOpts.near, this.cameraOpts.far); this.camera.position.z = this.cameraOpts.z; //SET-UP STAGE this.stage = new THREE.Scene(); this.stage.add(this.camera); //SET-UP RENDERER this.renderer = new THREE.WebGLRenderer({ antialias: true}); this.renderer.setSize(window.innerWidth, window.innerHeight); this.start(); } start(){ if (APP_CONFIG.debug){ this.debug(); } let texture = new THREE.Texture(this.createCanvas("Interactive Art Director")); texture.needsUpdate = true; let planeGeometry = new THREE.PlaneGeometry(this.planeWidth, this.planeHeight, 0, 0); this.uniforms = { texture: { type: 't', value: texture }, time: { type: "f", value: this.time }, mouseX: { type: "f", value: 0 }, mouseY: { type: "f", value: 0 } } var vertShader = document.getElementById('vertexShader').innerHTML; var fragShader = document.getElementById('fragmentShader').innerHTML; let planeMaterial = new THREE.ShaderMaterial({ uniforms: this.uniforms, vertexShader: vertShader, fragmentShader: fragShader, wireframe : false, wireframeLinewidth : 2, transparent : true }); this.plane = new THREE.Mesh(planeGeometry, planeMaterial); this.stage.add(this.plane); let container = document.querySelector('.stage'); container.appendChild(this.renderer.domElement); TweenMax.ticker.addEventListener('tick', this.animate); //ADD EVENTS LISTENER this.listen(); } createCanvas(text){ this.canvas = document.createElement( 'canvas' ); this.canvas.height = 4096; this.canvas.width = this.canvas.height*this.ratio; let context = this.canvas.getContext( '2d' ); context.beginPath(); context.rect(0, 0, this.canvas.width, this.canvas.height); context.fillStyle = '#202020'; context.fill(); context.closePath(); context.beginPath(); context.font = 'Bold '+ APP_CONFIG.fontSize +'px Avenir'; context.fillStyle = '#262626'; let width = context.measureText(text).width; context.fillText(text, this.canvas.width/2 - width/2, this.canvas.height/2); context.fill(); return this.canvas; } debug(){ let gridHelper = new THREE.GridHelper( APP_CONFIG.gridDebugSize.x, APP_CONFIG.gridDebugSize.y ); this.stage.add( gridHelper ); let axisHelper = new THREE.AxisHelper( APP_CONFIG.axisHelperSize ); this.stage.add( axisHelper ); } listen(){ let lazyLayout = _.debounce(this.onResize, 300); window.addEventListener('resize', lazyLayout); window.addEventListener('mousemove', this.onMouseMove); } onResize(e){ this.renderer.setSize(window.innerWidth, window.innerHeight); this.camera.updateProjectionMatrix(); } onMouseMove(e){ this.mousePos = { x: e.clientX, y: e.clientY } } animate(){ if(this.mousePos){ this.uniforms.mouseX.value = this.mousePos.x; this.uniforms.mouseY.value = window.innerHeight - this.mousePos.y; } this.renderer.render(this.stage, this.camera); this.uniforms.time.value += 0.1; } } new App();
ブラウザの画面キャプチャも載せておきます。
プログラム的なことは無知なため、非常識な質問の仕方をしていたら申し訳ありません。
何かヒントや解決方法ご存知の方、いらっしゃらないでしょうか?
また、参考サイトのコードではなく、こういった動きができる他のやりやすい実装方法があればご教授いただけますと幸いです。
まだ回答がついていません
会員登録して回答してみよう