Rails 5:JavaScript运行但不会出现在浏览器中

时间:2017-10-19 23:53:50

标签: javascript ruby-on-rails html5 canvas ruby-on-rails-5

我正在尝试在我的Rails 5应用程序中的canvas元素上运行一些JavaScript。我的canvas元素出现在相应的页面上,但我看不到JS。在确保问题不在rails资产管道之后,我添加了一些控制台日志消息,并且当使用开发人员工具时,我可以看到JS正在运行 - 它初始化并识别我的canvas元素。我已经能够在屏幕上看到JS运行几次,但无法找出共同的元素(确保它不是清除缓存/ cookie的问题)。我在我的根路径上使用JS,并且想知道路径是否在某种程度上是问题?非常感谢任何建议&智慧!

console.log("This is a test");

var particles = [];
var particleCount = 30;
var maxVelocity = 2;
var targetFPS = 33;

var canvasWidth = 400;
var canvasHeight = 400;

var imageObj = new Image();


imageObj.onload = function() {
    particles.forEach(function(particle) {
            particle.setImage(imageObj);
    });
};

imageObj.src = "http://www.blog.jonnycornwell.com/wp-content/uploads/2012/07/Smoke10.png";

function Particle(context) {
    this.x = 0;
    this.y = 0;

    this.xVelocity = 0;
    this.yVelocity = 0;

    this.radius = 5;

    this.context = context;

    this.draw = function() {

        if(this.image){
            this.context.drawImage(this.image, this.x-128, this.y-128);
            // If the image is being rendered do not draw the circle so break out of the draw function
            return;
        }
        this.context.beginPath();
        this.context.arc(this.x, this.y, this.radius, 0, 2 * Math.PI, false);
        this.context.fillStyle = "rgba(0, 255, 255, 1)";
        this.context.fill();
        this.context.closePath();
    };

    this.update = function() {
        this.x += this.xVelocity;
        this.y += this.yVelocity;

        if (this.x >= canvasWidth) {
            this.xVelocity = -this.xVelocity;
            this.x = canvasWidth;
        }

        else if (this.x <= 0) {
            this.xVelocity = -this.xVelocity;
            this.x = 0;
        }

        if (this.y >= canvasHeight) {
            this.yVelocity = -this.yVelocity;
            this.y = canvasHeight;
        }

        else if (this.y <= 0) {
            this.yVelocity = -this.yVelocity;
            this.y = 0;
        }
    };

    this.setPosition = function(x, y) {
        this.x = x;
        this.y = y;
    };

    this.setVelocity = function(x, y) {
        this.xVelocity = x;
        this.yVelocity = y;
    };

    this.setImage = function(image){
        this.image = image;
    }
}

function generateRandom(min, max){
    return Math.random() * (max - min) + min;
}

var context;

function init() {
    console.log("in init")
    var canvas = document.getElementById('myCanvas');
    console.log("canvas: " + canvas)
    if (canvas.getContext) {

        context = canvas.getContext('2d');

        for(var i=0; i < particleCount; ++i){
            var particle = new Particle(context);

            particle.setPosition(generateRandom(0, canvasWidth), generateRandom(0, canvasHeight));

            particle.setVelocity(generateRandom(-maxVelocity, maxVelocity), generateRandom(-maxVelocity, maxVelocity));
            particles.push(particle);
        }
    }
    else {
        alert("Please use a modern browser");
    }
}

function draw() {
    // Clear the drawing surface and fill it with a black background
    context.fillStyle = "rgba(0, 0, 0, 0.5)";
    context.fillRect(0, 0, 400, 400);

    // Go through all of the particles and draw them.
    particles.forEach(function(particle) {
        particle.draw();
    });
}

function update() {
    particles.forEach(function(particle) {
        particle.update();
    });
}


document.addEventListener("DOMContentLoaded", function(event){
  console.log("gets to init")
  init();
});

if (context) {
    setInterval(function() {
        // Update the scene befoe drawing
        update();

        // Draw the scene
        draw();
    }, 1000 / targetFPS);
}

HTML

 <% content_for :head do %>
   <%= javascript_include_tag "home.js" %>
 <% end %>

<body>
  <canvas id="myCanvas" width="400" height="400">
 </canvas>


</body>
</html>

Rails路线

  root to: 'products#home', as: "root"

再次感谢!

2 个答案:

答案 0 :(得分:0)

您使用的是turbolinks加载功能吗?只需在下面的代码中包装所有J:

$(document).on('turbolinks:load', function(){

答案 1 :(得分:0)

您可以使用以下其中一项:

Coffescript

$(document).on 'ready page:load', ->
  ...your code goes here...

Javascript(根据要求)

var ready = function() {
   ... your init code...
};
$(document).ready(ready);
$(document).on('page:load', ready);

$(document).on('ready page:load', function () {
   ...your code goes here...
});

使用Turbolinks

$(document).on('turbolinks:load', function() {
  ...your code goes here...
});