【问题标题】:How do I draw images (.png) to canvas in Cordova?如何在 Cordova 中将图像 (.png) 绘制到画布上?
【发布时间】:2015-10-04 14:42:00
【问题描述】:

我想在我的 Android 应用程序中绘制我自己绘制的猪和其他一些动物的图像。我以前从未尝试过制作应用程序,所以我不太确定该怎么做。我对 JavaScript 和 HTML 知之甚少。这是我的代码:(顶部在 Cordova 中是标准的,我不知道它是做什么的)

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady,     false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the     'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
};
app.initialize();





var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;

var pig = { x: 10, y: 10, w: 100, h: 100, img: new Image(), sound: new     Media("android_asset/www/sfx/pig.wav")};
var cow = { x: 130, y: 10, w: 100, h: 100, img: new Image(), sound: new     Media("android_asset/www/sfx/cow.wav")};
var cat = { x: 10, y: 130, w: 100, h: 100, img: new Image(), sound: new     Media("android_asset/www/sfx/cat.wav")};
var dog = { x: 130, y: 130, w: 100, h: 100, img: new Image(), sound: new     Media("android_asset/www/sfx/dog.wav")};

pig.img.src = "android_asset/www/img/pig.png"
cow.img.src = "android_asset/www/img/cow.png";
cat.img.src = "android_asset/www/img/cat.png";
dog.img.src = "android_asset/www/img/dog.png";

pig.sound.src = "../sfx/pig.wav";
cow.sound.src = "../sfx/cow.wav";
cat.sound.src = "../sfx/cat.wav";
dog.sound.src = "../sfx/dog.wav";

pig.sound.play();

function draw() {
    ctx.drawImage(pig.img, pig.x, pig.y);
    ctx.drawImage(cat.img, cat.x, cat.y);
    ctx.drawImage(cow.img, cow.x, cow.y);
    ctx.drawImage(dog.img, dog.x, dog.y);

    requestAnimationFrame(draw);
}
draw();

【问题讨论】:

    标签: javascript android cordova canvas


    【解决方案1】:

    onDeviceReady - 当 Cordova 完全加载时触发事件。

    您可以在应用准备就绪时通过向文档添加监听器来绘制画布:

    document.addEventListener("deviceready", draw, false);
    

    【讨论】:

    • 没有改变任何东西,我仍然只是得到一个灰屏。还有其他想法吗??
    • 对不起,我忘了通知:@Marius Balaj
    猜你喜欢
    • 2019-10-19
    • 2018-03-05
    • 1970-01-01
    • 2018-02-11
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多