LoginSignup
1

More than 5 years have passed since last update.

ShuffleEaselText.jsをeaseljs v0.6に対応

Posted at

こちら http://clockmaker.jp/blog/2012/02/html5_shuffletext/
easeljs v0.6で動かなかったので

ShuffleEaselText.js
/*
 * ShuffleText by Yasunobu Ikeda. Feb 3, 2012
 * Visit http://clockmaker.jp/ for documentation, updates and examples.
 *
 *
 * Copyright (c) 2012 Yasunobu Ikeda
 *
 * 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:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * 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.
 */

(function (window) {
    function ShuffleEaselText(text, font, color) {
        this.initialize(text, font, color);
        this.setText(text);
    }

    var p = ShuffleEaselText.prototype = new createjs.Text();

    p.sourceRandomCharacter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    p.emptyCharacter = " "; //"-";
    p.isRunning = false;
    p.duration = 600;
    p._orijinalStr = "";
    p._orijinalLength = "";
    p._timeCurrent = 0;
    p._timeStart = 0;
    p._randomIndex = [];

    p.setText = function (text) {
        this._orijinalStr = text;
        this._orijinalLength = text.length;
    };

    p.start = function () {
        this.stop();

        p._randomIndex = [];
        var str = "";
        for (var i = 0; i < this._orijinalLength; i++) {
            var rate = i / this._orijinalLength;
            p._randomIndex[i] = Math.random() * (1 - rate) + rate;
            str += this.emptyCharacter;
        }

        this._timeStart = new Date().getTime();
        this.addEventListener("tick", this.tickHandler);
        this.isRunning = true;

        this.text = str;
    };

    p.stop = function () {
        if (this.isRunning) {
            this.removeEventListener("tick", this.tickHandler);
        }
        this.isRunning = false;
    };

    p.tickHandler = function (event) {
        var that = event.target;
        that._timeCurrent = new Date().getTime() - that._timeStart;
        var percent = that._timeCurrent / that.duration;
        var str = "";
        var index;
        for (var i = 0; i < that._orijinalLength; i++) {
            if (percent >= p._randomIndex[i]) {
                str += that._orijinalStr.charAt(i);
            } else if (percent < p._randomIndex[i] / 3) {
                str += that.emptyCharacter;
            } else {
                index = Math.floor(Math.random() * (that.sourceRandomCharacter.length));
                str += that.sourceRandomCharacter.charAt(index);
            }
        }

        if (percent > 1) {
            str = that._orijinalStr;

            that.removeEventListener("tick", that.tickHandler);
            //that.removeAllEventListeners("tick");
            that.isRunning = false;

            that.dispatchEvent({type: "complete"}, that);
        }
        that.text = str;
    };

    window.ShuffleEaselText = ShuffleEaselText;
})(window);

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1