SVGで作る

桜の花びらの様なページ背景

ウェブサイトトップページの背景などに。

色相を変更:

Color1 Hue rotate Color2 Hue rotate
メインコード:
/**
 * さくら花びらの様なSVG画像出力
 *
 * @author ao-system, Inc.
 * @date 2024-02-03
 */

export class SakuraImage {
	'use strict';
	#transform = 20;
	#points = Array.from({length:8}, () => Math.random() * (this.#transform * 2) - this.#transform);
	#offsets = Array.from({length:8}, () => 0.2 + Math.random() * 0.2);	//speed
	#scaleDirection = 0;	//0:なし -1:小さくなる 1:大きくなる
	#scale = 1;
	#place;
	#color = ['rgba(255,255,255,0.5)','rgba(239,168,186,1)'];
	#filter = 'blur(5px) drop-shadow(0 0 5px #ffe)';
	#svgElm;
	#path;
	constructor(param) {
		if (param.place) {
			this.#place = param.place;
		}
		if (param.color) {
			this.#color = param.color;
		}
		if (param.filter) {
			this.#filter = param.filter;
		}
		this.#init();
	}
	set place(val) {
		if (this.#place instanceof Element) {
			this.#place.innerHTML = '';
		}
		this.#place = val;
		this.#createSvg();
	}
	set color(val) {
		if (this.#place instanceof Element) {
			this.#place.innerHTML = '';
		}
		this.#color = val;
		this.#createSvg();
	}
	set filter(val) {
		if (this.#place instanceof Element) {
			this.#place.innerHTML = '';
		}
		this.#filter = val;
		this.#createSvg();
	}
	#init() {
		this.#createSvg();
		this.#animateSvg();
	}
	//<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="200px" height="200px" viewBox="0 0 200 200">
	//	<radialGradient id="rg" cx="100" cy="100" r="50%" gradientUnits="userSpaceOnUse">
	//		<stop offset="0" style="stop-color:#7f6"/>
	//		<stop offset="1" style="stop-color:#ff9"/>
	//	</radialGradient>
	//	<path d="M170,66L128,72L135,30C9,41,32,168,32,168S160,191,170,66z"/>
	//</svg>
	#createSvg() {
		const radialGradientId = this.#generateRandomId();
		this.#svgElm = document.createElementNS('http://www.w3.org/2000/svg','svg');
		this.#svgElm.setAttribute('xmlns','http://www.w3.org/2000/svg');
		//this.#svgElm.setAttribute('width','200px');
		//this.#svgElm.setAttribute('height','200px');
		this.#svgElm.setAttribute('viewBox','0 0 200 200');
		this.#svgElm.style.filter = this.#filter;
		this.#svgElm.addEventListener('mouseover', () => {
			if (this.#scaleDirection == 0) {
				this.#scaleDirection = -1;
			}
		});
		this.#svgElm.style.transform = 'scale(' + this.#scale + ')';
		const radialGradient = document.createElementNS('http://www.w3.org/2000/svg','radialGradient');
		radialGradient.setAttribute('id',radialGradientId);
		radialGradient.setAttribute('cx',100);
		radialGradient.setAttribute('cy',100);
		radialGradient.setAttribute('r','40%');
		radialGradient.setAttribute('gradientUnits','userSpaceOnUse');
		const stop1 = document.createElementNS('http://www.w3.org/2000/svg','stop');
		stop1.setAttribute('offset',0);
		stop1.setAttribute('style','stop-color:' + this.#color[0]);
		const stop2 = document.createElementNS('http://www.w3.org/2000/svg','stop');
		stop2.setAttribute('offset',1);
		stop2.setAttribute('style','stop-color:' + this.#color[1]);
		this.#path = document.createElementNS('http://www.w3.org/2000/svg','path');
		this.#path.setAttribute('fill','url(#' + radialGradientId + ')');
		radialGradient.appendChild(stop1);
		radialGradient.appendChild(stop2);
		this.#svgElm.appendChild(radialGradient);
		this.#svgElm.appendChild(this.#path);
		this.#place?.appendChild(this.#svgElm);
	}
	#generateRandomId(idLength = 12) {
		const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		return Array.from({length:idLength}, () => characters[Math.floor(Math.random() * characters.length)]).join('');
	}
	#animateSvg() {
		if (this.#scaleDirection == -1) {
			this.#scale -= 0.005;
			this.#svgElm.style.transform = 'scale(' + this.#scale + ')';
			if (this.#scale < 0.5) {
				this.#scaleDirection = 1;
			}
		} else if (this.#scaleDirection == 1) {
			this.#scale += 0.005;
			this.#svgElm.style.transform = 'scale(' + this.#scale + ')';
			if (this.#scale >= 1) {
				this.#scaleDirection = 0;
			}
		}
		//
		for (let i = 0; i < this.#points.length; i++) {
			this.#points[i] += this.#offsets[i];
			if (this.#points[i] > this.#transform || this.#points[i] < -this.#transform) {
				this.#offsets[i] *= -1;
			}
		}
		const d = [
			`M${170 + this.#points[0]},${66 + this.#points[1]}`,
			`L${128 + this.#points[2]},${72 + this.#points[3]}`,
			`L${135 + this.#points[4]},${30 + this.#points[5]}`,
			`C9,41,${32 + this.#points[6]},${168 + this.#points[7]},${32 + this.#points[6]},${168 + this.#points[7]}`,
			`S160,191,${170 + this.#points[0]},${66 + this.#points[1]}`,
			`z`,
		].join('');
		this.#path.setAttribute('d',d);
		requestAnimationFrame(() => this.#animateSvg());
	}
}
スマートフォンなど非力なコンピュータでは処理が間に合わない場合が有ります。
処理性能に合わせて描画処理を切り分けるなど必要になります。
SVGとCSSを工夫すれば多彩な表現が可能。それをJavaScriptでアニメ―トする。
工夫次第で面白い表現が可能。サイトデザインやコンセプトに合わせて目を引く背景画像を作成しよう。
気まぐれコラム:
プログラマーまたはソフトウェアエンジニア業として特異なのは、ライブラリや開発環境の多くが無償で手に入るという点である。
これらを有難く使わせていただくのは問題ではないが、次のステップとして自身の知識やスキルを提供することも考えてみよう。これは業界全体をサポートすることであり、他の開発者への恩返しであり、同時に自らの更なるスキルアップにも繋がる。
提供された他の開発者のコードを見ることで、新しいアプローチやベストプラクティスを学び、成長することができるだろう。そして、創ることでプログラミングが単なる仕事ではなく、自分のアイディアを形にする喜びや楽しさを提供してくれるという本質的な価値を思い出すことができる。
新しいライブラリを使いこなすことも良い経験ではあるが、それ以上に重要なのは自ら考え、創り、そして提供することだ。生活のために短時間で作ることも原点ではあるが、自ら創り発信することも楽しさの原点ではないだろうか。
自ら創るとは言っても、プログラミング言語や環境は与えられたものであり、それらを組み替えているに過ぎない。よって完全オリジナルと言えるものではない。要は、感謝が大切ということだ。
(2024-02-03)
この記事は2024年2月当時の物です。
このサイトについてのお問い合わせはエーオーシステムまでお願いいたします。
ご使用上の過失の有無を問わず、本プログラムの運用において発生した損害に対するいかなる請求があったとしても、その責任を負うものではありません。