4

I have a page with a gradient applied on page load via CSS and would like to animate the page alternating the gradient colors and degree (linear-gradient - 4 different colors all degrade to white) on mouse move. If I use only 2 colors it works fine. But I want to get a random color from an array on mouse move but it flickers. Any solution for that?

Here's my Fiddle

var colorArr = ['#dfa7ca', '#f7c2b3', '#bae0f1', '#a6d6cb'];
var grFrom = colorArr[Math.floor(Math.random()*colorArr.length)];
var grTo = '#FFFFFF';

$("body").mousemove(function( e ) {
  var x = e.pageX - this.offsetLeft;
  var y = e.pageY - this.offsetTop;
  var grFrom = colorArr[Math.floor(Math.random()*colorArr.length)];//get a new random color
  var xy = (x + y) / 8;
  var w = $(this).width(),
  pct = 360*(+e.pageX)/w,
  bg = "linear-gradient(" + xy + "deg,"+grFrom+","+grTo+")";
  $("body").css("background-image", bg);
});
Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
mjpramos
  • 543
  • 5
  • 16
  • Try setting a `transition` css rule for your `body`. Something like `transition: background-image 0.5s ease;`. Edit: This is just a suggestion I didnt try it. – VTodorov Apr 11 '18 at 12:06
  • I think its normal to flicker, because you change the whole gradiant, I don't really get what you want to achieve, maybe you want some kind of animation? try to sue transition on background. – oma Apr 11 '18 at 12:06
  • You're selecting the color in the `mousemove` event, so every time the mouse moves, the color changes, which is a lot. So you want the color to change less often - when? – Ben West Apr 11 '18 at 12:07
  • I've tried the transition rule but I think I get the same flickering effect. Yes, if I could change the color only when user moves mouse to corners of the screen would be great. – mjpramos Apr 11 '18 at 12:18

2 Answers2

0

I think you need to inlcude jQueryUI and then you can use animate() to change the color. Please check here:

https://jqueryui.com/animate/

So you should use something like this:

 $( "#selector" ).animate({
          backgroundColor: bg
        }, 10 );
oma
  • 1,804
  • 12
  • 13
0

You can wrap your handler in _.throttle, and play with the millis, until you have an acceptable result.

var colorArr = ['#dfa7ca', '#f7c2b3', '#bae0f1', '#a6d6cb'];
var grFrom = colorArr[Math.floor(Math.random()*colorArr.length)];
var grTo = '#FFFFFF';


var wrapped = _.throttle(function(e){
  var x = e.pageX - this.offsetLeft;
  var y = e.pageY - this.offsetTop;
  var grFrom = colorArr[Math.floor(Math.random()*colorArr.length)];
  var xy = (x + y) / 8;
  var w = $(this).width(),
  pct = 360*(+e.pageX)/w,
  bg = "linear-gradient(" + xy + "deg,"+grFrom+","+grTo+")";
  $("body").css("background-image", bg);
},50)//Try increasing/decreasing this value to see the differrence


$("body").mousemove(wrapped);

See: https://jsfiddle.net/g137be71/24/

Update: Actually I had pasted a wrong link. This is for throttle. https://jsfiddle.net/g137be71/82/

Marinos An
  • 9,481
  • 6
  • 63
  • 96