PROWAREtech

articles » current » javascript » three-js » transition-from-one-color-to-another

ThreeJS: Transition from One Color to Another

How to gradually transition from one color to another using THREE.js with examples changing the scene background during the scroll event and animation event.

This snippet of code uses a linear transition.


// NOTE: Helper function
var INT2RGB = function (int) {
	return [(int & 0xff0000) >>> 16, (int & 0xff00) >>> 8, int & 0xff];
};

// NOTE: Helper function
var RGB2INT = function (rgb) {
	return (rgb[0] << 16) | (rgb[1] << 8) | rgb[2];
};

// NOTE: the function that transitions from one color to another based on a percentage of transition or completion
var transitionColor = function (percent, startColor, endColor) {
	if (percent < 0) {
		return startColor;
	}
	else if (percent > 100) {
		return endColor;
	}
	var pos = percent / 100;
	var rgb1 = INT2RGB(startColor);
	var rgb2 = INT2RGB(endColor);
	var r = Math.trunc((1 - pos) * rgb1[0] + pos * rgb2[0] + 0.5);
	var g = Math.trunc((1 - pos) * rgb1[1] + pos * rgb2[1] + 0.5);
	var b = Math.trunc((1 - pos) * rgb1[2] + pos * rgb2[2] + 0.5);
	return RGB2INT([r, g, b]);
};

Here is a working example that transitions during the animate function:


<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Color Transition</title>
	<style>
		body {
			margin: 0;
			padding: 0;
		}
		canvas {
			display: block;
			width: 100%;
			height: 100vh;
		}
	</style>
</head>
<body>
	<canvas></canvas>
	<script src="/js/three.min.js"></script>
	<script type="text/javascript">

(function () {

	var canvas = document.getElementsByTagName("canvas")[0];

	// NOTE: Helper function
	var INT2RGB = function (int) {
		return [(int & 0xff0000) >>> 16, (int & 0xff00) >>> 8, int & 0xff];
	};

	// NOTE: Helper function
	var RGB2INT = function (rgb) {
		return (rgb[0] << 16) | (rgb[1] << 8) | rgb[2];
	};

	// NOTE: the function that transitions from one color to another based on a percentage of transition or completion
	var transitionColor = function (percent, startColor, endColor) {
		if (percent < 0) {
			return startColor;
		}
		else if (percent > 100) {
			return endColor;
		}
		var pos = percent / 100;
		var rgb1 = INT2RGB(startColor);
		var rgb2 = INT2RGB(endColor);
		var r = Math.trunc((1 - pos) * rgb1[0] + pos * rgb2[0] + 0.5);
		var g = Math.trunc((1 - pos) * rgb1[1] + pos * rgb2[1] + 0.5);
		var b = Math.trunc((1 - pos) * rgb1[2] + pos * rgb2[2] + 0.5);
		return RGB2INT([r, g, b]);
	};

	// NOTE: create the scene to place objects in
	var scene = new THREE.Scene();
	scene.matrixWorldAutoUpdate = true;



	// NOTE: the width and height of the canvas
	var size = {
		width: canvas.offsetWidth,
		height: canvas.offsetHeight
	};


	var cameraNear = 1, cameraFar = 500;
	// NOTE: create the camera with 53 degree field of view; this is how the scene is viewed by the user
	var camera = new THREE.PerspectiveCamera(75, size.width / size.height, cameraNear, cameraFar);


	var renderer = new THREE.WebGLRenderer({
		canvas: canvas,
		antialias: true
	});
	renderer.setPixelRatio(window.devicePixelRatio);
	renderer.setSize(size.width, size.height);
	renderer.render(scene, camera);

	var percent = -50;

	// NOTE: MUST HAVE AN ANIMATE FUNCTION
	var animate = function () {
		scene.background = new THREE.Color(transitionColor(percent, 0x6699CC, 0xFF6633));
		percent += 0.5;
		renderer.render(scene, camera);
		requestAnimationFrame(animate);
	};
	animate();

})();

	</script>
</body>
</html>

Here is an example of transitioning between two colors during the scroll event:


<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Color Transition</title>
	<style>
		body {
			margin: 0;
			padding: 0;
			height: 300vh;
		}
		canvas {
			display: block;
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			height: 100vh;
		}
	</style>
</head>
<body>
	<canvas></canvas>
	<script src="/js/three.min.js"></script>
	<script type="text/javascript">

(function () {

	var canvas = document.getElementsByTagName("canvas")[0];

	// NOTE: Helper function
	var INT2RGB = function (int) {
		return [(int & 0xff0000) >>> 16, (int & 0xff00) >>> 8, int & 0xff];
	};

	// NOTE: Helper function
	var RGB2INT = function (rgb) {
		return (rgb[0] << 16) | (rgb[1] << 8) | rgb[2];
	};

	// NOTE: the function that transitions from one color to another based on a percentage of transition or completion
	var transitionColor = function (percent, startColor, endColor) {
		if (percent < 0) {
			return startColor;
		}
		else if(percent > 100) {
			return endColor;
		}
		var pos = percent / 100;
		var rgb1 = INT2RGB(startColor);
		var rgb2 = INT2RGB(endColor);
		var r = Math.trunc((1 - pos) * rgb1[0] + pos * rgb2[0] + 0.5);
		var g = Math.trunc((1 - pos) * rgb1[1] + pos * rgb2[1] + 0.5);
		var b = Math.trunc((1 - pos) * rgb1[2] + pos * rgb2[2] + 0.5);
		return RGB2INT([r, g, b]);
	};

	// NOTE: create the scene to place objects in
	var scene = new THREE.Scene();
	scene.matrixWorldAutoUpdate = true;



	// NOTE: the width and height of the canvas
	var size = {
		width: canvas.offsetWidth,
		height: canvas.offsetHeight
	};


	var cameraNear = 1, cameraFar = 500;
	// NOTE: create the camera with 53 degree field of view; this is how the scene is viewed by the user
	var camera = new THREE.PerspectiveCamera(75, size.width / size.height, cameraNear, cameraFar);
	camera.position.z = 3;

	var renderer = new THREE.WebGLRenderer({
		canvas: canvas,
		antialias: true
	});
	renderer.setPixelRatio(window.devicePixelRatio);
	renderer.setSize(size.width, size.height);
	renderer.render(scene, camera);

	var planeImage = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAYAAAB5fY51AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAACYBJREFUeNrs3Y914kYCwOHh3jXAlUBKYEtgS7BLwCXgErwlQAl2CaYEu4R1CXYJBL0bJcThjzQaCUl833t6ySWXNRbSj9EghslutwsAQ/AfuwAQLADBAgQLQLAABAsQLADBAhAsQLAABAtAsADBAhAsAMECBAtAsAAECxAsAMECECxAsAAEC0CwAMECECwAwQIEC0CwAMECECwAwQIEC0CwAAQLECwAwQIQLECwAAQLQ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MKoxW+hT8W3jxEawb8X7hlbftpW6uLXXUUWWUtQn9mOQu3mBws6hgjcKld8u6WOpmiJeFxfxXlXnFNj5KVff59c6gYI3CY8VLvuKE+zni/ZB6Qle99eJa0XoIw/xiEATraKzqrPNURmuMl4dtf4vyV9x3XY10yqWcxUqwRuE+pC1KV0TrjzC+S4zUG27nod4NnOUyQV8t/y4/XAYK1lhGEk2DU756/wzjegexyd3pdZ+DNlYx/Th4XkywC1Y3drvdpMEI6FRgHmOo7jMezOUr+c/MJ982Pt6uRwipc0xVJ9+PXZKXK200eU5e4vM6xpHvTZvU+HhLfx70ZHK4ztSswiv6y8HIZxO6nXMqLo8WByOPSx9hOfxq+2247WVPDp/nc/uu/BLXoX7T9jUHA4IFcLOXhACCBQgWgGABggUgWACCBQgWgGABCBYgWACCBSBYgGABCBaAYAGCBSBYAIIFCBaAYAEIFiBYAIIFIFiAYAEIFoBgAYIFIFgAggUIFoBgAYIFIFgAggUIFoBgAQgWIFgAggUgWIBgAQgWgGABggUgWACCBQgWgGABCBYgWACCBSBYgGABCBaAYAGCBSBYAIIFCBaAYAGCBSBYAIIFCBaAYAEIFiBYAIIFIFiAYAEIFoBgAYIFIFgAggUIFoBgAQgWIFgAggUgWIBgAQgWgGABggUgWACCBQgWgGABggUgWACCBQgWgGABCBYgWACCBSBYgGABCBaAYAGCBSBYAIIFCBaAYAEIFiBYAIIFIFiAYAEIFoBgAYIFIFgAggUIFoBgAYIFIFgAggUIFoBgATTzpwADAGtM5LEaxvPvAAAAAElFTkSuQmCC";
	var planeGeometry = new THREE.PlaneGeometry(1, 1);
	var planeMaterial = new THREE.MeshBasicMaterial({ map: new THREE.TextureLoader().load(planeImage), transparent: true, side: THREE.DoubleSide });
	scene.add(new THREE.Mesh(planeGeometry, planeMaterial));

	scene.background = new THREE.Color(0xFF0000);

	// NOTE: the parent element is the document body!!!
	window.onscroll = function() {
		var scrollPosition = document.body.offsetHeight / canvas.offsetHeight / 2 * window.scrollY;
		var percent = scrollPosition / document.body.offsetHeight * 100;
		scene.background = new THREE.Color(transitionColor(percent, 0xFF0000, 0xFFFF00));
	};

	// NOTE: MUST HAVE AN ANIMATE FUNCTION
	var animate = function () {
		renderer.render(scene, camera);
		requestAnimationFrame(animate);
	};
	animate();

})();

	</script>
</body>
</html>

PROWAREtech

Hello there! How can I help you today?
Ask any question

PROWAREtech

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE