質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

CSS

CSSはXMLやHTMLで表現した色・レイアウト・フォントなどの要素を指示する仕様の1つです。

Q&A

解決済

2回答

1295閲覧

【JavaScript】テキストにhoverしたとき、ぐにゃっとする動きを実装(参考コードあり)

退会済みユーザー

退会済みユーザー

総合スコア0

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

CSS

CSSはXMLやHTMLで表現した色・レイアウト・フォントなどの要素を指示する仕様の1つです。

0グッド

1クリップ

投稿2021/11/10 05:31

こんにちは。
普段はデザイン担当で簡単な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();

ブラウザの画面キャプチャも載せておきます。
イメージ説明

プログラム的なことは無知なため、非常識な質問の仕方をしていたら申し訳ありません。
何かヒントや解決方法ご存知の方、いらっしゃらないでしょうか?
また、参考サイトのコードではなく、こういった動きができる他のやりやすい実装方法があればご教授いただけますと幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

■ Blotter.js
https://github.com/bradley/blotter

というライブラリを用いることで、多少コードを簡略化できるようだったので、参考までに載せておきます。

html

1<script src="https://rawcdn.githack.com/bradley/Blotter/28a89fd0d5fd22ef46e01590df8cb3664c5a9ec2/build/blotter.js"></script> 2 3<script id="fs" type="x-shader/x-fragment"> 4void mainImage( out vec4 mainImage, in vec2 fragCoord ) 5{ 6 vec2 uv = fragCoord.xy / uResolution.xy; 7 float d = distance(vec2(mouseX,mouseY), uv); 8 float r = dot(fragCoord.xy, vec2(0.00005,0.00005))/d; 9 vec2 tex = vec2(uv.x + r, uv.y + r); 10 mainImage = textTexture(tex); 11} 12</script>

javascript

1var text = new Blotter.Text("こんにちは", { 2 family : "serif", 3 size : 120, 4 fill : "#171717" 5}); 6 7var mainImageSource = document.getElementById("fs").textContent; 8var material = new Blotter.ShaderMaterial(mainImageSource, { 9 uniforms : { 10 mouseX: {type :"1f", value : 0.0}, 11 mouseY: {type :"1f", value : 0.0} 12 } 13}); 14 15var blotter = new Blotter(material, { texts : text }); 16var scope = blotter.forText(text); 17scope.appendTo(document.body); 18 19scope.on("mousemove", function(mousePosition) { 20 scope.material.uniforms.mouseX.value = mousePosition.x; 21 scope.material.uniforms.mouseY.value = mousePosition.y; 22});

<実行結果>
https://jsfiddle.net/cx20/0a5wtxzu/

イメージ説明

投稿2021/11/10 17:13

編集2021/11/11 13:50
cx20

総合スコア4633

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2021/11/11 07:22

ご回答ありがとうございます。簡略化できるのですね。 参考にさせていただきます!
cx20

2021/11/11 14:12

更にコードを短くしてみました。 Blotter.jsも内部的にThree.jsを呼び出している為、Three.jsのサンプルと本質的な内容は変わりません。 仕組みとしては、 <処理概要> 1. 任意の文字列を Canvas に描画(WebGL のテクスチャに使用) 2. WebGL + GLSL(シェーダ言語)を用いてテクスチャに対してエフェクトを実行 3. マウス座標をエフェクトのプログラムに引き渡し → 2~3 を繰り返し といった感じです。 テキストの内容を変更するだけであれば1の個所を修正すれば良いですが、 エフェクトの内容自体カスタマイズしたい場合は2の部分(GLSLのコード)を覚える必要があるかと思います。 参考になるか分かりませんが、以前 GLSL を試した際のサンプルがありますので、リンクを貼っておきます。興味があるようでしたらどうぞ。 https://github.com/cx20/glsl-test
guest

0

ベストアンサー

Appクラスのメンバ関数createCanvasに書かれているところの処理でcanvasに背景や文字を描画しているようですね。なので、そこだけ1行ずつ確認してみましょう。

javascript

1createCanvas(text){ 2 // canvasタグを作成している 3 this.canvas = document.createElement( 'canvas' ); 4 // 作成したcanvasタグの高さを4096pxに変更 5 this.canvas.height = 4096; 6 // 作成したcanvasタグの幅を高さとアスペクト比から算出 7 this.canvas.width = this.canvas.height*this.ratio; 8 // canvasタグのコンテキストを取得(canvasへの秒はこのcontextを使って描画します) 9 let context = this.canvas.getContext( '2d' ); 10 11 // 背景色の描画(beginPath 〜 closePathまでが背景色描画の処理) 12 // 背景の描画はcanvas全体に矩形を描画して実現している 13 // ------------------------------------------------------------ 14 15 // 図形の描画開始 16 context.beginPath(); 17 // (left: 0, top: 0, right: width, bottom: height) の矩形をCanvasに描画 18 context.rect(0, 0, this.canvas.width, this.canvas.height); 19 // 描画する矩形の色を決める(これが背景色。ここを色々いじれば) 20 context.fillStyle = '#202020'; 21 // 矩形を塗りつぶす 22 context.fill(); 23 // 図形の描画終了 24 context.closePath(); // 矩形の描画開始 25 26 // テキストの描画(beginPath 〜 closePathまでがテキスト描画の処理) 27 // ------------------------------------------------------------ 28 29 // 図形の描画開始 30 context.beginPath(); 31 // フォント指定 32 context.font = 'Bold '+ APP_CONFIG.fontSize +'px Avenir'; 33 // 文字の色を決定 34 context.fillStyle = '#262626'; 35 // 描画するテキストの幅を取得 36 let width = context.measureText(text).width; 37 // widthとcanvas.widthを使って真ん中にテキストを描画 38 context.fillText(text, this.canvas.width/2 - width/2, this.canvas.height/2); 39 // 図形の描画終了 40 context.fill(); 41 42 // canvasを返却 43 return this.canvas; 44 45}

とりあえず↑を読んでもらえたら、背景色の色の変え方とテキストの色の変え方はわかるかなと思います。
ほかの操作(サイズ変えたり)は、↑のコードは全てHTML5のCanvasのAPIしか利用していないので、「html5 canvas」とかで調べやり方を見つけららえると思います。

また、このプログラムでは主にThree.jsでCG描画しているようなので、Three.jsについても調べてみるとある程度コードが読めるようになるかもしれません。

投稿2021/11/10 10:19

ukyoda

総合スコア386

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2021/11/11 07:10

初めまして。 とてもご丁寧に説明頂き、ありがとうございます、、!!助かります。 頂きましたコメントをみて、少しいじってみようかと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問