willowbreezeaaurelte1nimation
Version:
This project provides a simple JavaScript code snippet to generate a wind-blown willow tree animation effect. The animation consists of a tree trunk and a leaf that sways gently back and forth.
169 lines (150 loc) • 5.13 kB
JavaScript
function createLeaf(width, height, backgroundColor, positionLeft, positionBottom, borderRadius) {
var leaf = document.createElement('div');
leaf.style.width = width;
leaf.style.height = height;
leaf.style.backgroundColor = backgroundColor;
leaf.style.position = 'absolute';
leaf.style.left = positionLeft;
leaf.style.bottom = positionBottom;
leaf.style.borderRadius = borderRadius;
leaf.style.transformOrigin = 'bottom center';
return leaf;
}
function generateRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function generateRandomSize(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min + 'px';
}
function generateRandomPosition(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min + '%';
}
function generateRandomBorderRadius() {
return Math.floor(Math.random() * 50) + '%';
}
function generateLeaves(numLeaves) {
for (var i = 0; i < numLeaves; i++) {
var leaf = createLeaf(generateRandomSize(30, 80), generateRandomSize(30, 80), generateRandomColor(), generateRandomPosition(0, 100), generateRandomPosition(20, 100), generateRandomBorderRadius());
treeContainer.appendChild(leaf);
}
}
generateLeaves(10);
function updateLeaves() {
for (var i = 2; i < treeContainer.childNodes.length; i++) {
var leaf = treeContainer.childNodes[i];
var angle = Math.random() * Math.PI * 2;
var distance = Math.random() * 50 + 50;
var xOffset = Math.cos(angle) * distance;
var yOffset = Math.sin(angle) * distance;
leaf.style.transform = 'translate(' + xOffset + 'px, ' + yOffset + 'px)';
}
requestAnimationFrame(updateLeaves);
}
updateLeaves();
function generateRandomRotation() {
return Math.floor(Math.random() * 360) + 'deg';
}
function generateRandomOpacity() {
return Math.random().toFixed(2);
}
function generateRandomScale() {
return Math.random().toFixed(2);
}
function generateRandomBorder() {
var borderWidth = Math.floor(Math.random() * 10) + 'px';
var borderStyleArray = ['solid', 'dotted', 'dashed', 'double'];
var borderStyle = borderStyleArray[Math.floor(Math.random() * borderStyleArray.length)];
var borderColor = generateRandomColor();
return borderWidth + ' ' + borderStyle + ' ' + borderColor;
}
function generateRandomTextShadow() {
var hShadow = Math.floor(Math.random() * 10) + 'px';
var vShadow = Math.floor(Math.random() * 10) + 'px';
var blurRadius = Math.floor(Math.random() * 10) + 'px';
var shadowColor = generateRandomColor();
return hShadow + ' ' + vShadow + ' ' + blurRadius + ' ' + shadowColor;
}
function shuffleArray(array) {
let currentIndex = array.length, temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function generateRandomString(length) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function deepCopyObject(obj) {
let copy;
if (null == obj || "object" != typeof obj) return obj;
if (obj instanceof Date) {
copy = new Date();
copy.setTime(obj.getTime());
return copy;
}
if (obj instanceof Array) {
copy = [];
for (let i = 0, len = obj.length; i < len; i++) {
copy[i] = deepCopyObject(obj[i]);
}
return copy;
}
if (obj instanceof Object) {
copy = {};
for (let attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = deepCopyObject(obj[attr]);
}
return copy;
}
throw new Error("Unable to copy obj! Its type isn't supported.");
}
function findDuplicatesInArray(array) {
let duplicates = [];
let compare = [];
for (let i = 0; i < array.length; i++) {
if (!compare.includes(array[i])) {
compare.push(array[i]);
} else {
duplicates.push(array[i]);
}
}
return duplicates;
}
function mergeSort(arr) {
if (arr.length < 2)
return arr;
var middle = parseInt(arr.length / 2);
var left = arr.slice(0, middle);
var right = arr.slice(middle, arr.length);
return merge(mergeSort(left), mergeSort(right));
}
function merge(left, right) {
var result = [];
while (left.length && right.length) {
if (left[0] <= right[0]) {
result.push(left.shift());
} else {
result.push(right.shift());
}
}
while (left.length)
result.push(left.shift());
while (right.length)
result.push(right.shift());
return result;
}