/    Sign up×
Community /Pin to ProfileBookmark

Want to run a javascript on the size of the page

I’m trying to get a javascript run on the size of the entire page.

The script was taken from: [url]http://codepen.io/soulwire/pen/Ffvlo[/url]

I changed it quite a bit to get the desired effect to do what I wanted.

[url]www.insania.co[/url]

The site runs on cargo, so everything had to be placed into the user created HTML. What I’d like to achieve is to get the ‘background’ effect to fill out the entire screen with perhaps margins.

If you look at the website and go to the “Your Mom” page (I’m sorry ? ) you can see that the actual script stops halfway through the page.

I tried a javascript (first few lines that give an alert) to save the window size into a variable and use that to define the NUM_PARTICLE, but it doesn’t work. The closest I could get is using screen.height / width.

Is there a way to perhaps scale the div to the screen size (CSS width=100%; didn’t work either), or ‘scale the script’? Because changing particle density or spacing can make the website awful heavy.

I hope it’s clear so far, if you have questions ask !

[code=html]<body>

<script src=’https://gist.github.com/mrdoob/838785/raw/a19a753b441d6ad41707c58f06dbe17f3470423c/RequestAnimationFrame.js’></script>
<script src=’https://raw.github.com/mrdoob/stats.js/master/build/stats.min.js’></script>
<script type=”text/javascript”>

var body = document.body,
html = document.documentElement;

var height = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );

alert(“hello world. The value of Height is: ” + height);

var NUM_PARTICLES = ( ( ROWS = screen.height/6 ) * ( COLS = screen.width/8 ) ),
THICKNESS = Math.pow( 80, 2.8 ),
SPACING = 8,
MARGIN = 100,
COLOR = 250,
DRAG = 0.95,
EASE = 0.05,

/*

used for sine approximation, but Math.sin in Chrome is still fast enough :)http://jsperf.com/math-sin-vs-sine-approximation

B = 4 / Math.PI,
C = -4 / Math.pow( Math.PI, 2 ),
P = 0.225,

*/

container,
particle,
canvas,
mouse,
stats,
list,
ctx,
tog,
man,
dx, dy,
mx, my,
d, t, f,
a, b,
i, n,
w, h,
p, s,
r, c
;

particle = {
vx: 0,
vy: 0,
x: 0,
y: 0
};

function init() {

container = document.getElementById( ‘container’ );
canvas = document.createElement( ‘canvas’ );

ctx = canvas.getContext( ‘2d’ );
man = false;
tog = true;

list = [];

w = canvas.width = COLS * SPACING + MARGIN ;
h = canvas.height = ROWS * SPACING + MARGIN ;

container.style.marginLeft = Math.round( w * -0.5 ) + ‘px’;
container.style.marginTop = Math.round( h * -0.5 ) + ‘px’;

for ( i = 0; i < NUM_PARTICLES; i++ ) {

p = Object.create( particle );
p.x = p.ox = MARGIN + SPACING * ( i % COLS );
p.y = p.oy = MARGIN + SPACING * Math.floor( i / COLS );

list[i] = p;
}

container.addEventListener( ‘mousemove’, function(e) {

bounds = container.getBoundingClientRect();
mx = e.clientX – bounds.left;
my = e.clientY – bounds.top;
man = true;

});

if ( typeof Stats === ‘function’ ) {
document.body.appendChild( ( stats = new Stats() ).domElement );
}

container.appendChild( canvas );
}

function step() {

if ( stats ) stats.begin();

if ( tog = !tog ) {

if ( !man ) {

t = +new Date() * 0.001;
mx = w * 0.5 + ( Math.cos( t * 2.1 ) * Math.cos( t * 0.9 ) * w * 0.45 );
my = h * 0.5 + ( Math.sin( t * 3.2 ) * Math.tan( Math.sin( t * 0.8 ) ) * h * 0.45 );
}

for ( i = 0; i < NUM_PARTICLES; i++ ) {

p = list[i];

d = ( dx = mx – p.x ) * dx + ( dy = my – p.y ) * dy;
f = -THICKNESS / d;

if ( d < THICKNESS ) {
t = Math.atan2( dy, dx );
p.vx += f * Math.cos(t);
p.vy += f * Math.sin(t);
}

p.x += ( p.vx *= DRAG ) + (p.ox – p.x) * EASE;
p.y += ( p.vy *= DRAG ) + (p.oy – p.y) * EASE;

}

} else {

b = ( a = ctx.createImageData( w+15, h+5 ) ).data;

for ( i = 0; i < NUM_PARTICLES; i++ ) {

p = list[i];
b[n = ( ~~p.x + ( ~~p.y * w ) ) * 4] = b[n+1] = b[n+2] = COLOR, b[n+3] = 50;
}

ctx.putImageData( a, 0, 0 );
}

if ( stats ) stats.end();

requestAnimationFrame( step );
}

init();
step();
</script>

<div id=’container’></div>[/code]

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumSep 11.2015 — Loading of www.insania.co fails for me.
Copy linkTweet thisAlerts:
@anonyzeroauthorSep 11.2015 — Loading of www.insania.co fails for me.[/QUOTE]

not sure what the issue is, chrome / edge / chromium / safari all run the site for me.
Copy linkTweet thisAlerts:
@SempervivumSep 11.2015 — I see, an "m" ist missing: www.insania.com is correct.
Copy linkTweet thisAlerts:
@anonyzeroauthorSep 11.2015 — the domain is .co not .com, so that should be correct. Perhaps you have JS popups blocked and won't show the site because of that. I've disabled the alert now
Copy linkTweet thisAlerts:
@SempervivumSep 11.2015 — Yes, this link loads.

Your HTML is faulty: Multiple <!DOCTYPE html>, <html >, <head> and <body>. Apparently you have copied a complete HTML page into you document.
Copy linkTweet thisAlerts:
@anonyzeroauthorSep 11.2015 — Yes, this link loads.

Your HTML is faulty: Multiple <!DOCTYPE html>, <html >, <head> and <body>. Apparently you have copied a complete HTML page into you document.[/QUOTE]


should be fixed now! The script is also twice in the page, but the first one shouldn't work. Still doesn't resolve my problem though
Copy linkTweet thisAlerts:
@SempervivumSep 11.2015 — Yes, the script is duplicate. You should remove one version.
×

Success!

Help @anonyzero spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...