简体   繁体   English

通过Javascript和HTML5 Canvas更新背景图片

[英]Update background image through Javascript and HTML5 Canvas

Javascript is very far from being one of my strengths; Javascript远非我的优势之一; therefore apologies if my question appears very remedial to many of you. 因此,如果我的问题对你们许多人来说非常有用,那么道歉。

I am attempting to implement Boids through Javascript and the HTML5 Canvas. 我试图通过Javascript和HTML5 Canvas实现Boids。 Although the flocking behaviour has been established, the application looks rather plain upon a blank background. 尽管已经建立了植绒行为,但应用程序在空白背景上看起来相当明显。

For this reason, I am attempting to use an image as my background, but am having difficulties. 出于这个原因,我试图使用图像作为我的背景,但我遇到了困难。

this.draw = function() {
        /* First clear everything */
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);

        /* And ink them */
        this.context.strokeStyle = "#000";
        this.context.stroke();

        /* Draw each boid */
        for(var i in this.boids) {
            this.boids[i].draw(this.context, this.size);
        }
    }

If anyone has knowledge of how to paint a background image within the above function, please can you let me know :) 如果有人知道如何在上述功能中绘制背景图像,请告诉我:)

Instead of doing it in Canvas, you can more easily set a background-image in the CSS: 您可以更轻松地在CSS中设置background-image ,而不是在Canvas中执行此操作:

http://jsfiddle.net/qwzBh/ http://jsfiddle.net/qwzBh/

That way you won't have to worry about drawing it all the time. 这样你就不必担心一直都在画它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM