こんにちは。
普段はデザイン担当で簡単なHTML,CSSしか触ったことがないのですが、
急遽私の方でJSを使った動きのあるテキストをHPの見出しとして実装することとなりました。
下記のURLのようなマウスをhverするとぐにゃっとなる動きを使用したいです。
下記サイトのコードをまるまるもってきて、実装するところまでは出来ました。
https://goworkship.com/magazine/text-effect-typography/#23
これから背景を削除したり、テキストの大きさや色、位置を変えたりしたいのですが、
どの部分を変更すればよいかわからず困っています。
なんとか調べ、文字はjsファイルのテキスト部分を変えると変わることが分かったのですが。。。
下記、私が記載しているソースコードです。
html
1<!DOCTYPE html> 2 3<html lang="en"> 4<head> 5 <meta charset="UTF-8"> 6 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 7 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 8 <title>Document</title> 9 <link rel="stylesheet" href="style.css"> 10 <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"></script> 11 <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> 12 <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.19.0/TweenMax.min.js"></script> 13 14</head> 15<body> 16 <script id="fragmentShader" type="x-shader/x-fragment"> 17 18 precision mediump float; 19 20 uniform float time; 21 uniform float mouseX; 22 uniform float mouseY; 23 uniform sampler2D texture; 24 varying vec2 vUv; 25 26 void main() { 27 float d = -distance(vec2(mouseX,mouseY), gl_FragCoord.xy); 28 float r = dot(gl_FragCoord.xy, vec2(0.005,0.005))/d; 29 vec2 tex = vec2(vUv.x + r, vUv.y + r); 30 gl_FragColor = vec4(texture2D(texture, tex).rgb, 1.0); 31 } 32 33 </script> 34 35 <script id="vertexShader" type="x-shader/x-vertex"> 36 precision mediump float; 37 varying vec2 vUv; 38 uniform float time; 39 void main() { 40 vUv = uv; 41 gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); 42 } 43 </script> 44 45 <div class="stage"></div> 46 <canvas class="noise-canvas"></canvas> 47 <script src="test.js"></script> 48</body> 49</html>
CSS
1.noise-canvas{ 2 position: absolute; 3 top: 0; 4 left: 0; 5 bottom: 0; 6 right: 0; 7 width: 100%; 8 height: 100%; 9 opacity: 0.3; 10 z-index: 2; 11}
JS
1// Copyright (c) 2021 by Corentin (https://codepen.io/corentinfardeau/pen/VmZBpv) 2// 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: 3// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 4// 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. 5 6'use strict' 7 8console.clear(); 9const APP_CONFIG = { 10 debug: false, 11 gridDebugSize: { 12 x: 10, 13 y: 10 14 }, 15 fontSize: 600, 16 axisHelperSize: 10 17} 18 19class App { 20 21 constructor(){ 22 _.bindAll(this, 'animate', 'onResize', 'onMouseMove'); 23 24 this.time = 0; 25 26 this.planeHeight = 50; 27 this.ratio = window.innerWidth / window.innerHeight; 28 this.planeWidth = this.planeHeight*this.ratio; 29 //SET-UP CAMERA 30 this.cameraOpts = { 31 aspect: window.innerWidth / window.innerHeight, 32 near: 0.1, 33 far : 10000, 34 z: this.planeHeight 35 } 36 37 let fov = 2 * Math.atan( this.planeHeight / ( 2 * this.cameraOpts.z ) ) * ( 180 / Math.PI ); 38 39 this.camera = new THREE.PerspectiveCamera(fov, this.cameraOpts.aspect, this.cameraOpts.near, this.cameraOpts.far); 40 this.camera.position.z = this.cameraOpts.z; 41 42 //SET-UP STAGE 43 this.stage = new THREE.Scene(); 44 this.stage.add(this.camera); 45 46 //SET-UP RENDERER 47 this.renderer = new THREE.WebGLRenderer({ antialias: true}); 48 this.renderer.setSize(window.innerWidth, window.innerHeight); 49 50 this.start(); 51 } 52 53 start(){ 54 55 if (APP_CONFIG.debug){ 56 this.debug(); 57 } 58 let texture = new THREE.Texture(this.createCanvas("Interactive Art Director")); 59 texture.needsUpdate = true; 60 61 let planeGeometry = new THREE.PlaneGeometry(this.planeWidth, this.planeHeight, 0, 0); 62 this.uniforms = { 63 texture: { 64 type: 't', 65 value: texture 66 }, 67 time: { 68 type: "f", 69 value: this.time 70 }, 71 mouseX: { 72 type: "f", 73 value: 0 74 }, 75 mouseY: { 76 type: "f", 77 value: 0 78 } 79 } 80 81 var vertShader = document.getElementById('vertexShader').innerHTML; 82 var fragShader = document.getElementById('fragmentShader').innerHTML; 83 84 let planeMaterial = new THREE.ShaderMaterial({ 85 uniforms: this.uniforms, 86 vertexShader: vertShader, 87 fragmentShader: fragShader, 88 wireframe : false, 89 wireframeLinewidth : 2, 90 transparent : true 91 }); 92 93 this.plane = new THREE.Mesh(planeGeometry, planeMaterial); 94 this.stage.add(this.plane); 95 96 let container = document.querySelector('.stage'); 97 container.appendChild(this.renderer.domElement); 98 TweenMax.ticker.addEventListener('tick', this.animate); 99 100 //ADD EVENTS LISTENER 101 this.listen(); 102 } 103 104 createCanvas(text){ 105 106 this.canvas = document.createElement( 'canvas' ); 107 this.canvas.height = 4096; 108 this.canvas.width = this.canvas.height*this.ratio; 109 let context = this.canvas.getContext( '2d' ); 110 111 context.beginPath(); 112 context.rect(0, 0, this.canvas.width, this.canvas.height); 113 context.fillStyle = '#202020'; 114 context.fill(); 115 context.closePath(); 116 117 context.beginPath(); 118 context.font = 'Bold '+ APP_CONFIG.fontSize +'px Avenir'; 119 context.fillStyle = '#262626'; 120 let width = context.measureText(text).width; 121 context.fillText(text, this.canvas.width/2 - width/2, this.canvas.height/2); 122 context.fill(); 123 124 return this.canvas; 125 126 } 127 128 debug(){ 129 let gridHelper = new THREE.GridHelper( APP_CONFIG.gridDebugSize.x, APP_CONFIG.gridDebugSize.y ); 130 this.stage.add( gridHelper ); 131 let axisHelper = new THREE.AxisHelper( APP_CONFIG.axisHelperSize ); 132 this.stage.add( axisHelper ); 133 } 134 135 listen(){ 136 let lazyLayout = _.debounce(this.onResize, 300); 137 window.addEventListener('resize', lazyLayout); 138 window.addEventListener('mousemove', this.onMouseMove); 139 } 140 141 onResize(e){ 142 this.renderer.setSize(window.innerWidth, window.innerHeight); 143 this.camera.updateProjectionMatrix(); 144 } 145 146 onMouseMove(e){ 147 this.mousePos = { 148 x: e.clientX, 149 y: e.clientY 150 } 151 } 152 153 animate(){ 154 155 if(this.mousePos){ 156 this.uniforms.mouseX.value = this.mousePos.x; 157 this.uniforms.mouseY.value = window.innerHeight - this.mousePos.y; 158 } 159 160 this.renderer.render(this.stage, this.camera); 161 this.uniforms.time.value += 0.1; 162 } 163} 164 165new App();
プログラム的なことは無知なため、非常識な質問の仕方をしていたら申し訳ありません。
何かヒントや解決方法ご存知の方、いらっしゃらないでしょうか?
また、参考サイトのコードではなく、こういった動きができる他のやりやすい実装方法があればご教授いただけますと幸いです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2021/11/11 07:22
2021/11/11 14:12