第8回 インタラ会 資料

テーマは「ランダム」


サンプルファイルはこちら↓↓ github.com

ランダム関数を使った動き

・みんな大好き
・実案件だとノイズ表現とかで使用する
・やりすぎると目が痛い
・作品作りの肝は何をランダムにするか
・目が痛くないギリギリを目指す
・もしくは振り切る

ランダムな動きを実装するのに便利な関数

// ----------------------------------------
// minからmaxまでランダム
// ----------------------------------------
function random(min, max) {
  return Math.random() * (max - min) + min;
}

// ----------------------------------------
// minからmaxまでランダム int
// ----------------------------------------
function randomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

// ----------------------------------------
// -valからvalまでランダム
// ----------------------------------------
function range(val) {
  return random(-val, val);
}

// ----------------------------------------
// 配列の中ランダム
// ----------------------------------------
function randomArr(arr) {
  return arr[randomInt(0, arr.length - 1)]
}

// 1 / rangeの確率でtrueを取得
// -----------------------------------
// @range : 2以上の分母(int)
// return : true or false(boolean)
// -----------------------------------
function hit(range) {
  return (randomInt(0, range - 1) == 0)
}

// ----------------------------------------
// minからmaxまでランダム 半分の確率で-をつける
// ----------------------------------------
function random2(min, max) {
  var val = Math.random() * (max - min) + min;
  if(Math.random() > 0.5) {
    val *= -1;
  }
  return val;
}

スケール、角度をランダムに

See the Pen intr8 sample1 by ikeryou (@ikeryou) on CodePen.

ソースはこちら

色をランダムに

See the Pen intr8 sample2 by ikeryou (@ikeryou) on CodePen.

ソースはこちら

テキストをランダムに

See the Pen intr8 sample3 by ikeryou (@ikeryou) on CodePen.

ソースはこちら

SVGをランダムに

See the Pen intr8 sample4 by ikeryou (@ikeryou) on CodePen.

ソースはこちら




インタラ会とは?

ikeryou.hatenablog.com