Cotton.JS is a JavaScript library that allows you to make a mouse interaction animation easily.
npm install cottonjs
import Cotton.JS in your own project :
import Cotton from 'cottonjs'
Simply download and include with a script tag :
<script src="cotton.min.js"></script>
or using CDN :
<script src="https://cdn.jsdelivr.net/gh/cotton123236/cottonjs@latest/lib/cotton.min.js"></script>
Create an element that you would like to animate.
<div id="cotton-cursor"></div>
Style your element.
There is some rules that you need to notice.
#cotton-cursor {
position: fixed;
top: 0;
left: 0;
z-index: 9;
width: 20px;
height: 20px;
border-radius: 50%;
pointer-events: none;
background-color: #f9cabc;
transform: translate(-20px, -20px);
}
Initialize Cotton in JavaScript.
const cottonCursor = new Cotton('#cotton-cursor')
See some demos.
Knowing more details about these parameters.
All available parameters :
const cottonCursor = new Cotton('#cotton-cursor', {
scene: 'body', // element
models: '.cotton-model', //elements
cottonInitClass: 'cotton-init',
cottonMovingClass: 'cotton-moving',
cottonActiveClass: 'cotton-active',
modelsActiveClass: 'model-active',
speed: 0.125,
centerMouse: true,
airMode: {
resistance: 15,
reverse: false,
alive: false
}
})
Knowing more details about these methods.
All available methods :
const cotton = new Cotton('#cotton-cursor')
// call the method after initialization.
cotton.stop()
cotton.move()
cotton.updateModels()
// call the method in callbacks
const cotton = new Cotton('#cotton-cursor', {
on: {
enterModel() {
this.stop()
this.move()
this.updateModels()
}
}
})
Knowing more details about these callbacks.
All available callbacks :
const cotton = new Cotton('#cotton-cursor', {
on: {
// callbacks defined on here.
enterModel(cotton, model, event) {},
leaveModel(cotton, model, event) {},
enterScene(cotton, scene, event) {},
leaveScene(cotton, scene, event) {},
cottonMove(cotton, event) {}
}
})
Because there is no mouse on mobile devices, Cotton.JS will automatically detect user device and won't initialize when using mobile.
- Visit the website
- Released under the MIT License.
- ©2021 cotton123236