久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

失敗的 Canvas 360 jquery 插件

Failing Canvas 360 jquery plugin(失敗的 Canvas 360 jquery 插件)
本文介紹了失敗的 Canvas 360 jquery 插件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

這和我來的一樣近..

但在加載圖像時它會中斷 - 只有第二個畫布似乎可以渲染?

but its breaking when loading in the images -- only the 2nd canvas appears to render?

問題似乎出現在 img.load 回調期間

the problem seems to occur during the img.load callback

https://jsfiddle.net/7a4738jo/24/

html..

<script type="text/javascript" src="https://code.createjs.com/easeljs-0.6.0.min.js"></script>
<canvas id="one" data-init="table" width="465" height="465">1</canvas>
<canvas id="two" data-init="table" width="465" height="465">2</canvas>

js...

(function ($) {
    var defaults = {
        string1: "hello ",
        string2: "world!"
    };
    var methods = {
        init: function (options) {
            if (options) {
                $.extend(defaults, options);
            }
            //console.log("defaults", defaults);
            methods.start(this);
            //console.log(defaults.string1 + defaults.string2);
        },
        test: function (arg) {
            console.log("test: " + arg.args);
            console.log("args: " + defaults.string1 + defaults.string2);
        },
        reInit: function () {
            this.loaded = 0; //reset
            this.render();
            this.load360Image(false);
        },
        setCursor: function (cursor) {
            document.body.style.cursor = cursor;
        },
        render: function () {
            //load in a new render
            this.fileCount = this.el.data("file-count");
            this.path = this.el.data("file-path");
            this.startFrame = 10;

            //create img list
            this.imgList = [];

            /*
            for (i = 1; i <= this.fileCount; i++) {
                this.imgList.push(this.path + i + ".png");
            }*/

            this.imgList = ["http://jsrun.it/assets/N/b/D/X/NbDXj.jpg", 
                   "http://jsrun.it/assets/f/K/7/y/fK7yE.jpg", 
                   "http://jsrun.it/assets/j/U/q/d/jUqdG.jpg", 
                   "http://jsrun.it/assets/q/o/4/j/qo4jP.jpg", 
                   "http://jsrun.it/assets/i/Q/e/1/iQe1f.jpg", 
                   "http://jsrun.it/assets/5/k/y/R/5kyRi.jpg", 
                   "http://jsrun.it/assets/x/T/I/h/xTIhA.jpg", 
                   "http://jsrun.it/assets/4/X/G/F/4XGFt.jpg", 
                   "http://jsrun.it/assets/6/7/n/r/67nrO.jpg", 
                   "http://jsrun.it/assets/k/i/r/8/kir8T.jpg", 
                   "http://jsrun.it/assets/2/3/F/q/23Fqt.jpg", 
                   "http://jsrun.it/assets/c/l/d/5/cld59.jpg", 
                   "http://jsrun.it/assets/e/J/O/f/eJOf1.jpg", 
                   "http://jsrun.it/assets/o/j/Z/x/ojZx4.jpg", 
                   "http://jsrun.it/assets/w/K/2/m/wK2m3.jpg", 
                   "http://jsrun.it/assets/w/K/2/m/wK2m3.jpg", 
                   "http://jsrun.it/assets/4/b/g/V/4bgVf.jpg", 
                   "http://jsrun.it/assets/4/m/1/8/4m18z.jpg", 
                   "http://jsrun.it/assets/4/w/b/F/4wbFX.jpg", 
                   "http://jsrun.it/assets/4/k/T/G/4kTGQ.jpg", 
                   "http://jsrun.it/assets/s/n/C/r/snCrr.jpg", 
                   "http://jsrun.it/assets/7/f/H/u/7fHuI.jpg", 
                   "http://jsrun.it/assets/v/S/d/F/vSdFm.jpg", 
                   "http://jsrun.it/assets/m/g/c/S/mgcSp.jpg", 
                   "http://jsrun.it/assets/t/L/t/P/tLtPF.jpg", 
                   "http://jsrun.it/assets/j/7/e/H/j7eHx.jpg", 
                   "http://jsrun.it/assets/m/o/8/I/mo8Ij.jpg", 
                   "http://jsrun.it/assets/n/P/7/h/nP7ht.jpg", 
                   "http://jsrun.it/assets/z/f/K/S/zfKSP.jpg", 
                   "http://jsrun.it/assets/2/3/4/U/234U6.jpg", 
                   "http://jsrun.it/assets/d/Z/y/m/dZymk.jpg"];


            this.totalFrames = this.imgList.length;
        },
        start: function (element) {
            var self = this;
            console.log(">>>element",element);
            this.el = element;

            this.canvas = this.el[0];
            if (!this.canvas || !this.canvas.getContext) {
                return;
            }
            console.log(">>>methods.canvas",this.canvas);

            this.stage = new createjs.Stage(this.canvas);
            this.stage.enableMouseOver(true);
            this.stage.mouseMoveOutside = true;
            createjs.Touch.enable(methods.stage);

            this.images = [];
            this.loaded = 0;
            this.currentFrame = 0
            this.rotate360Interval;
            this.start_x;

            this.bg = new createjs.Shape();
            this.stage.addChild(this.bg);

            this.bmp = new createjs.Bitmap();
            this.stage.addChild(this.bmp);

            var myTxt = new createjs.Text("HTC One", '24px Ubuntu', "#ffffff");
            myTxt.x = myTxt.y = 20;
            myTxt.alpha = 0.08;
            this.stage.addChild(myTxt);

            this.setCursor("progress");

            this.render();
            //console.log(this.el[0]);
            this.load360Image(true);

            // TICKER
            createjs.Ticker.addEventListener("tick", function (event) {
                self.stage.update();
            });
            createjs.Ticker.setFPS(60);
            createjs.Ticker.useRAF = true;

            return this.canvas;
        },
        load360Image: function (spin) {
            var img = new Image();
            //img.crossOrigin = "Anonymous";

            img.src = this.imgList[this.loaded];

            this.img360Loaded(img, spin);
            this.images[this.loaded] = img;
        },
        img360Loaded: function (img, spin) {
            //console.log("LOAD IMG", this.el[0], img);
            var that = this;
            img.onload = function (event) {
                //console.log(img);

                that.loaded++;
                that.bg.graphics.clear()
                that.bg.graphics.beginFill("#222").drawRect(0, 0, that.stage.canvas.width * that.loaded / that.totalFrames, that.stage.canvas.height);
                that.bg.graphics.endFill();


                if (that.loaded == that.totalFrames) {
                    that.start360(spin);
                } else {
                    that.load360Image(spin);
                }

                //console.log(">>LOAD IMG DONE", that.el[0]);
            }
        },
        start360: function (spin) {
            this.setCursor("none");

            // 360 icon
            var iconImage = new Image();
            //iconImage.crossOrigin = "Anonymous";
            iconImage.src = this.el.data("icon-path") + "360.png";
            this.iconLoaded(iconImage);

            // update-draw
            this.update360(0);

            if (spin) {
                this.revolveOnce(function () {
                    //console.log("done");
                }, 25);
            }
            this.setCursor("auto");
        },
        revolveOnce: function (callback, speed) {
            var self = this;
            // first rotation
            this.rotate360Interval = setInterval(function () {
                if (self.currentFrame === self.totalFrames - 1) {
                    clearInterval(self.rotate360Interval);
                    self.addNavigation();
                }
                self.update360(1);

                if (self.currentFrame === self.totalFrames - 1) {
                    callback();
                }
            }, speed);
        },
        iconLoaded: function (iconImage) {
            var self = this;
            iconImage.onload = function (event) {
                var iconBmp = new createjs.Bitmap();
                iconBmp.image = event.target;
                iconBmp.x = 20;
                iconBmp.y = self.canvas.height - iconBmp.image.height - 70;
                self.stage.addChild(iconBmp);
            }
        },
        setFrame: function (newFrame) {
            this.bmp.image = this.images[newFrame];
        },
        addNavigation: function () {

            var self = this;

            this.stage.onMouseOver = function (event) {
                self.setCursor("pointer");
            }

            this.stage.onMouseDown = function (event) {
                self.start_x = event.rawX;
                self.stage.onMouseMove = self.mouseMoved;

                self.stage.onMouseMove = function (event) {
                    var dx = event.rawX - self.start_x;
                    var abs_dx = Math.abs(dx);

                    if (abs_dx > 5) {
                        self.update360(dx / abs_dx);
                        self.start_x = event.rawX;
                    }
                }

                self.stage.onMouseUp = function (event) {
                    self.stage.onMouseMove = null;
                    self.stage.onMouseUp = null;
                    self.setCursor("pointer");
                    core.changeOnStage(this, self.currentFrame);
                }

                self.setCursor("w-resize");
            }


            this.setCursor("auto");
        },
        update360: function (dir) {
            this.currentFrame += dir;

            if (this.currentFrame < 0) {
                this.currentFrame = this.totalFrames - 1;
            } else if (this.currentFrame > this.totalFrames - 1) {
                this.currentFrame = 0;
            }
            this.bmp.image = this.images[this.currentFrame];
        }
    };
    $.fn.dataTable = function (method) {
        var args = arguments;
        var $this = this;
        return this.each(function () {
            if (methods[method]) {
                return methods[method].apply($this, Array.prototype.slice.call(args, 1));
            } else if (typeof method === 'object' || !method) {
                return methods.init.apply($this, Array.prototype.slice.call(args, 0));
            } else {
                $.error('Method ' + method + ' does not exist on jQuery.plugin');
            }
        });
    };

})(jQuery);


// Init
$(document).ready(function () {
    //create multiple instances of canvas
    $('[data-init="table"]').each(function (index) {
        var instance = $(this).dataTable();
        console.log("instance", instance);
    });

    $("#two").dataTable("test", {
        args: "test args passed"
    });
});

推薦答案

你可以把它做成一個widget,而不是使用plugin的方法.你不需要太多的改變,它會更靈活.看到這里,幾乎沒有改變你的代碼:

Instead of using plugin approach, you could make it a widget. You wouldn't need too much changes and it would be more flexible. See here, with almost no change to your code:

 $(function () {

    $.widget("custom.threesixty", {
        // default options
        options: {

        },

        // the constructor
        _create: function () {
            this.start(this.element);
        },


        _refresh: function () {

        },


        // events bound via _on are removed automatically
        // revert other modifications here
        _destroy: function () {

        },

        // _setOptions is called with a hash of all options that are changing
        // always refresh when changing options
        _setOptions: function () {
            // _super and _superApply handle keeping the right this-context
            this._superApply(arguments);
            this._refresh();
        },

        // _setOption is called for each individual option that is changing
        _setOption: function (key, value) {

            this._super(key, value);
        },
        setCursor: function (cursor) {
            document.body.style.cursor = cursor;
        },
        render: function () {
            //load in a new render
            this.fileCount = this.el.data("file-count");
            this.path = this.el.data("file-path");
            this.startFrame = 10;

            //create img list
            this.imgList = [];

            /*
            for (i = 1; i <= this.fileCount; i++) {
                this.imgList.push(this.path + i + ".png");
            }*/

            this.imgList = ["http://jsrun.it/assets/N/b/D/X/NbDXj.jpg",
                "http://jsrun.it/assets/f/K/7/y/fK7yE.jpg",
                "http://jsrun.it/assets/j/U/q/d/jUqdG.jpg",
                "http://jsrun.it/assets/q/o/4/j/qo4jP.jpg",
                "http://jsrun.it/assets/i/Q/e/1/iQe1f.jpg",
                "http://jsrun.it/assets/5/k/y/R/5kyRi.jpg",
                "http://jsrun.it/assets/x/T/I/h/xTIhA.jpg",
                "http://jsrun.it/assets/4/X/G/F/4XGFt.jpg",
                "http://jsrun.it/assets/6/7/n/r/67nrO.jpg",
                "http://jsrun.it/assets/k/i/r/8/kir8T.jpg",
                "http://jsrun.it/assets/2/3/F/q/23Fqt.jpg",
                "http://jsrun.it/assets/c/l/d/5/cld59.jpg",
                "http://jsrun.it/assets/e/J/O/f/eJOf1.jpg",
                "http://jsrun.it/assets/o/j/Z/x/ojZx4.jpg",
                "http://jsrun.it/assets/w/K/2/m/wK2m3.jpg",
                "http://jsrun.it/assets/w/K/2/m/wK2m3.jpg",
                "http://jsrun.it/assets/4/b/g/V/4bgVf.jpg",
                "http://jsrun.it/assets/4/m/1/8/4m18z.jpg",
                "http://jsrun.it/assets/4/w/b/F/4wbFX.jpg",
                "http://jsrun.it/assets/4/k/T/G/4kTGQ.jpg",
                "http://jsrun.it/assets/s/n/C/r/snCrr.jpg",
                "http://jsrun.it/assets/7/f/H/u/7fHuI.jpg",
                "http://jsrun.it/assets/v/S/d/F/vSdFm.jpg",
                "http://jsrun.it/assets/m/g/c/S/mgcSp.jpg",
                "http://jsrun.it/assets/t/L/t/P/tLtPF.jpg",
                "http://jsrun.it/assets/j/7/e/H/j7eHx.jpg",
                "http://jsrun.it/assets/m/o/8/I/mo8Ij.jpg",
                "http://jsrun.it/assets/n/P/7/h/nP7ht.jpg",
                "http://jsrun.it/assets/z/f/K/S/zfKSP.jpg",
                "http://jsrun.it/assets/2/3/4/U/234U6.jpg",
                "http://jsrun.it/assets/d/Z/y/m/dZymk.jpg"];


            this.totalFrames = this.imgList.length;
        },
        reInit: function () {
            this.loaded = 0; //reset
            this.render();
            this.load360Image(false);
        },
        start: function (element) {
            var self = this;
            console.log(">>>element", element);
            this.el = element;

            this.canvas = this.el[0];
            if (!this.canvas || !this.canvas.getContext) {
                return;
            }
            console.log(">>>methods.canvas", this.canvas);

            this.stage = new createjs.Stage(this.canvas);
            this.stage.enableMouseOver(true);
            this.stage.mouseMoveOutside = true;
            createjs.Touch.enable(this.stage);

            this.images = [];
            this.loaded = 0;
            this.currentFrame = 0
            this.rotate360Interval;
            this.start_x;

            this.bg = new createjs.Shape();
            this.stage.addChild(this.bg);

            this.bmp = new createjs.Bitmap();
            this.stage.addChild(this.bmp);

            var myTxt = new createjs.Text("HTC One", '24px Ubuntu', "#ffffff");
            myTxt.x = myTxt.y = 20;
            myTxt.alpha = 0.08;
            this.stage.addChild(myTxt);

            this.setCursor("progress");

            this.render();
            //console.log(this.el[0]);
            this.load360Image(true);

            // TICKER
            createjs.Ticker.addEventListener("tick", function (event) {
                self.stage.update();
            });
            createjs.Ticker.setFPS(60);
            createjs.Ticker.useRAF = true;

            return this.canvas;
        },
        load360Image: function (spin) {
            var img = new Image();
            //img.crossOrigin = "Anonymous";

            img.src = this.imgList[this.loaded];

            this.img360Loaded(img, spin);
            this.images[this.loaded] = img;
        },
        img360Loaded: function (img, spin) {
            console.log("LOAD IMG", this.el[0], img);
            var that = this;
            img.onload = function (event) {
                //console.log(img);

                that.loaded++;
                that.bg.graphics.clear()
                that.bg.graphics.beginFill("#222").drawRect(0, 0, that.stage.canvas.width * that.loaded / that.totalFrames, that.stage.canvas.height);
                that.bg.graphics.endFill();


                if (that.loaded == that.totalFrames) {
                    that.start360(spin);
                } else {
                    that.load360Image(spin);
                }

                //console.log(">>LOAD IMG DONE", that.el[0]);
            }
        },
        start360: function (spin) {
            this.setCursor("none");

            // 360 icon
            var iconImage = new Image();
            //iconImage.crossOrigin = "Anonymous";
            iconImage.src = this.el.data("icon-path") + "360.png";
            this.iconLoaded(iconImage);

            // update-draw
            this.update360(0);

            if (spin) {
                this.revolveOnce(function () {
                    //console.log("done");
                }, 25);
            }
            this.setCursor("auto");
        },
        revolveOnce: function (callback, speed) {
            var self = this;
            // first rotation
            self.rotate360Interval = setInterval(function () {
                if (self.currentFrame === self.totalFrames - 1) {
                    clearInterval(self.rotate360Interval);
                    self.addNavigation();
                }
                self.update360(1);

                if (self.currentFrame === self.totalFrames - 1) {
                    callback();
                }
            }, speed);
        },
        iconLoaded: function (iconImage) {
            var self = this;
            iconImage.onload = function (event) {
                var iconBmp = new createjs.Bitmap();
                iconBmp.image = event.target;
                iconBmp.x = 20;
                iconBmp.y = self.canvas.height - iconBmp.image.height - 70;
                self.stage.addChild(iconBmp);
            }
        },
        setFrame: function (newFrame) {
            this.bmp.image = this.images[newFrame];
        },
        addNavigation: function () {

            var self = this;

            self.stage.onMouseOver = function (event) {
                self.setCursor("pointer");
            }

            self.stage.onMouseDown = function (event) {
                self.start_x = event.rawX;
                self.stage.onMouseMove = self.mouseMoved;

                self.stage.onMouseMove = function (event) {
                    var dx = event.rawX - self.start_x;
                    var abs_dx = Math.abs(dx);

                    if (abs_dx > 5) {
                        self.update360(dx / abs_dx);
                        self.start_x = event.rawX;
                    }
                }

                self.stage.onMouseUp = function (event) {
                    self.stage.onMouseMove = null;
                    self.stage.onMouseUp = null;
                    self.setCursor("pointer");
                    self.changeOnStage(this, self.currentFrame);
                }

                self.setCursor("w-resize");
            }


            this.setCursor("auto");
        },
        update360: function (dir) {
            this.currentFrame += dir;

            if (this.currentFrame < 0) {
                this.currentFrame = this.totalFrames - 1;
            } else if (this.currentFrame > this.totalFrames - 1) {
                this.currentFrame = 0;
            }
            this.bmp.image = this.images[this.currentFrame];
        }
    });
    $('[data-init="table"]').threesixty()
    $("#trigger").click(function () {
        console.log("try other method");
        $("#two").data("file-path", "mobile4").data("file-count", 20);
        $("#two").threesixty("reInit");
    });
})

http://jsfiddle.net/e4vLyt5n/7/

這使您可以訪問 jQuery-ui 的通用方法,例如銷毀、設置選項等.請參閱此處的文檔:https://jqueryui.com/widget/

This gives you access to generic methods of jQuery-ui such as destroy, set options, etc. See doc here: https://jqueryui.com/widget/

基本上,你調用這樣的方法:

Basically, you call methods like this:

$(element).threesixty(method, args)

所以你可以這樣做:

$(element).threesixty('instance')//to access the instance
$(element).threesixty('start')//to call start method
$(element).threesixty('option', key, value)//to set options one it's instantiated
$(element).threesixty({
    option1: value,
    option2: value,
});//to set options when creating it

等等

這篇關于失敗的 Canvas 360 jquery 插件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

jQuery/JavaScript Library for avatar creation?(用于創建頭像的 jQuery/JavaScript 庫?)
How to do following mask input problem?(如何做以下掩碼輸入問題?)
Issues Setting Value/Label Using DropKick Javascript(使用 DropKick Javascript 設置值/標簽的問題)
how to unit-test private methods in jquery plugins?(如何對 jquery 插件中的私有方法進行單元測試?)
stellar.js - configuring offsets / aligning elements for a vertical scrolling website?(stellar.js - 為垂直滾動網站配置偏移量/對齊元素?)
jQuery masked input plugin. select all content when textbox receives focus(jQuery 屏蔽輸入插件.當文本框獲得焦點時選擇所有內容)
主站蜘蛛池模板: 国产欧美日韩在线观看 | 看av网址 | 免费视频中文字幕 | 亚洲少妇综合网 | 91色在线| 亚洲人免费视频 | 日日骚网| 亚洲国产一区二区三区, | 精品久久精品 | 在线三级网址 | 黑人精品xxx一区一二区 | 国内精品一区二区三区 | 成人一区二区视频 | 欧美精品一区二区三区四区 | 亚洲综合小视频 | 毛色毛片免费看 | www.4567 | 国产一级在线 | 国产高清视频在线观看 | 日韩精品久久久久 | 欧美日韩久久 | 伊人导航| 日本精品免费在线观看 | 亚洲日本欧美日韩高观看 | 一级黄色片免费在线观看 | 国产日韩精品久久 | 精品久久久久久国产 | 国产一区不卡 | 亚洲视频免费观看 | 三级在线观看 | 一级做a爰片久久毛片 | 日韩视频精品在线 | 欧美日本韩国一区二区 | av日韩在线播放 | 毛片在线免费 | 日韩免费在线 | 日韩精品网站 | 亚洲最新在线 | 亚洲一区二区三区乱码aⅴ 四虎在线视频 | 一区二区三区国产好 | 欧美一区二区三区在线免费观看 |