[pixi.js] ブレンドモード

pixijsを使って、ブレンドモードを学習するよ。 :bouzu:

pixijs_blendmode

MovieClipクラスLoaderクラスで表示してみたよ。 :boy:

左から順に、NORMAL, ADD, MULTIPLY, SCREEN, OVERLAY, DARKEN, LIGHTEN を指定。
あれ? OVERLAY, DARKEN, LIGHTEN が効いてない? 😕

index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>BlendMode [basic] | pixi.js</title>
<script src="js/pixi.min.js"></script>
<script src="js/stats.min.js"></script>

<style type="text/css">
  body {
    margin: 0;
    padding: 0;
  }
</style>

<script>
var stage, renderer, stats;
var loader;
var piyotextures;

function init() {
  stats = new Stats();
  stats.setMode(0);
  stats.domElement.style.position = "fixed";
  stats.domElement.style.right = "0px";
  stats.domElement.style.top = "0px";
  document.body.appendChild(stats.domElement);

  stage = new PIXI.Container();
  renderer = PIXI.autoDetectRenderer(600, 200, {backgroundColor: 0xFFFFFF});
  document.body.appendChild(renderer.view);
  renderer.view.style.display = "block";
  renderer.view.style.width = "600px";
  renderer.view.style.marginTop = "40px";
  renderer.view.style.marginLeft = "auto";
  renderer.view.style.marginRight = "auto";
  renderer.view.style.paddingLeft = "0";
  renderer.view.style.paddingRight = "0";

  loader = new PIXI.loaders.Loader();
  loader.add("piyo", "assets/piyo.json");
  loader.on("complete", complete);
  loader.load();

  update();
}
function update() {
  renderer.render(stage);
  requestAnimationFrame(update);

  stats.update();
}
function complete() {
  background();
  initialize();
}
function initialize() {
  piyotextures = [];
  for (var n = 0; n < 60; n++) {
    var texture = PIXI.Texture.fromFrame("piyo" + n + ".png");
    piyotextures.push(texture);
  }

  var piyo1 = createPiyo(0);
  piyo1.blendMode = PIXI.BLEND_MODES.NORMAL;

  var piyo2 = createPiyo(1);
  piyo2.blendMode = PIXI.BLEND_MODES.ADD;

  var piyo3 = createPiyo(2);
  piyo3.blendMode = PIXI.BLEND_MODES.MULTIPLY;

  var piyo4 = createPiyo(3);
  piyo4.blendMode = PIXI.BLEND_MODES.SCREEN;

  var piyo5 = createPiyo(4);
  piyo5.blendMode = PIXI.BLEND_MODES.OVERLAY;

  var piyo6 = createPiyo(5);
  piyo6.blendMode = PIXI.BLEND_MODES.DARKEN;

  var piyo7 = createPiyo(6);
  piyo7.blendMode = PIXI.BLEND_MODES.LIGHTEN;
}
function createPiyo(n) {
  var piyo = new PIXI.extras.MovieClip(piyotextures);
  stage.addChild(piyo);
  piyo.anchor.x = 0.5;
  piyo.anchor.y = 0.9;
  piyo.position.x = 75 + 75*n;
  piyo.position.y = 175;
  piyo.gotoAndPlay(Math.random()*60);
  return piyo;
}
function background() {
  var sky = new PIXI.Graphics();
  stage.addChild(sky);
  sky.beginFill(0x00AAE4);
  sky.drawRect(0, 0, 600, 150);
  sky.endFill();

  var ground = new PIXI.Graphics();
  stage.addChild(ground);
  ground.beginFill(0x99CC33);
  ground.drawRect(0, 150, 600, 50);
  ground.endFill();

  var version = PIXI.VERSION;
  var rendererType;
  switch (renderer.type) {
    case PIXI.RENDERER_TYPE.WEBGL :
      rendererType = "WebGL";
      break;
    case PIXI.RENDERER_TYPE.CANVAS :
      rendererType = "Context2D";
      break;
  }
  var txt = "HTML5 / pixi.js [" + version + "] (" + rendererType + ")";
  var basic = new PIXI.Text(txt, {"font": "14px Arial", "fill": "#FFFFFF", "align": "left"});
  stage.addChild(basic);
  basic.position.x = 4;
  basic.position.y = 2;
  basic.alpha = 0.6;
  var title = new PIXI.Text("BlendMode", {"font": "24px Arial", "fill": "#FFFFFF", "align": "center"});
  stage.addChild(title);
  title.position.x = 300 - (title.width >> 1);
  title.position.y = 40;
  title.alpha = 0.6;
  var subtitle = new PIXI.Text("[basic]", {"font": "20px Arial", "fill": "#FFFFFF", "align": "center"});
  stage.addChild(subtitle);
  subtitle.position.x = 300 - (subtitle.width >> 1);
  subtitle.position.y = 84;
  subtitle.alpha = 0.6;
  var publish = new PIXI.Text("authoring: Sublime Text 2 + Flash CC", {"font": "14px Arial", "fill": "#FFFFFF", "align": "right"});
  stage.addChild(publish);
  publish.position.x = 600 - (publish.width + 4);
  publish.position.y = 2;
  publish.alpha = 0.6;
}
</script>
</head>
<body onload="init();" style="background-color:#EEEEEE">
</body>
</html>


[修正] (15/03/01 Sun 16:47)
pixi.js v2.2.7 にバージョンアップ。
[修正] (15/04/28 Tue 16:58)
pixi.js v3.0.2 にバージョンアップ。
PIXI.AssetLoader を PIXI.loaders.Loader に変更。PIXI.MovieClip を PIXI.extras.MovieClip に変更。PIXI.DisplayObjectContainer を PIXI.Container に変更。PIXI.blendModes を PIXI.BLEND_MODES に変更。
:caution: ローカルで確認すると、次のようなエラーがでる。
Error: The frameId “****.png” does not exist in the texture cache
[修正] (15/05/10 Sun 15:22)
pixi.js v3.0.3 にバージョンアップ。
ローカルで発生していたエラーがSafariでは出なくなった。