Insert a javascript background in wordpress theme

Viewed 95

I want to put a javascript file as my background on my wordpress website. It is an animated background that is continuously runing (it's the matrix rain). I don't know where should i start to insert the code, i have been looking on internet but there is no tutorial about inserting an animated background on a wordpress theme.

Could you help me please? I think a lot of people are in my situation.

Thanks :)

1 Answers

I take this matrix-rain library as an example https://sigstart.github.io/matrix-rain/.

First you have to save the file https://raw.githubusercontent.com/sigstart/matrix-rain/master/matrix.js as matrix-rain.js in the js folder of the theme.

Then you have to include the JavaScript file.

In functions.php

// Import moment.js
wp_enqueue_script('moment-min-js', 'https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js', array(), '1.0.0', false);
// Import matrix-rain
wp_enqueue_script('matrix-rain', get_template_directory_uri() . '/js/matrix-rain.js', array(), '1.0.0', false);

Then in the template where you want to show the background, for example single-post.php put this:

<canvas id="matrix"></canvas>
Related