Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
` A code project with no squishy text.Using the sping-mass model,the text is converted to a particle seperated by springs.The particle should behave like soft body physics as springs.
The i simply no output of the spring.

What I have tried:

I have tried to reposition the text.
``html
<!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>Squishy Text</title>
    <script src="./index.js" type="module" defer></script>
</head>

<body>
    <div id="canvas-container">
        <canvas id="canvas"></canvas>
    </div>
</body>
</html>

```
```js
// Canvas setup
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');

const width = canvas.width = window.innerWidth;
const height = canvas.height = window.innerHeight;

// Particle and spring setup
const particles = [];
const springs = [];
const point = [0, 0];

const particleRadius = 3;
const springLength = 100;
const springK = 0.1;


// Create particles from traced text
ctx.font = "152px serif";
ctx.fillStyle = "red";
const options = {
    text: {
        data: "Squishy"
    }
}
const tracedPoints = [];
const tracedData = ctx.getImageData(0, 0, ctx.measureText(options.text.data).width, 152);
for (let y = 0; y < tracedData.height; y++) {
    for (let x = 0; x < tracedData.width; x++) {
        const index = (y * tracedData.width + x) * 4;
        if (tracedData.data[index + 3] > 0) {
            tracedPoints.push([x, y]);
        }
    }
}

function drawSpring(context, pointA, pointB, stiffness, damping, amplitude, frequency) {
    const dx = pointB.x - pointA.x;
    const dy = pointB.y - pointA.y;
    const distance = Math.sqrt(dx * dx + dy * dy);
    const springLength = distance;

    // Calculate spring force
    const springForce = (distance - springLength) * stiffness;

    // Calculate damping force
    const dampingForce = damping * (pointB.vx - pointA.vx);

    const totalForce = springForce + dampingForce;

    // Calculate acceleration
    const accelerationA = { x: (springForce * dx / distance - dampingForce * pointA.vx) / pointA.mass, y: (springForce * dy / distance - dampingForce * pointA.vy) / pointA.mass };
    const accelerationB = { x: -totalForce / pointB.mass, y: -totalForce / pointB.mass };

    // Update velocity
    pointA.vx += accelerationA.x;
    pointA.vy += accelerationA.y;
    pointB.vx += accelerationB.x;
    pointB.vy += accelerationB.y;

    // Update position
    pointA.x += pointA.vx;
    pointA.y += pointA.vy;
    pointB.x += pointB.vx;
    pointB.y += pointB.vy;

    // Draw spring
    context.beginPath();
    context.moveTo(pointA.x, pointA.y);
    const numSegments = 20;
    const segmentLength = distance / numSegments;
    for (let i = 1; i < numSegments; i++) {
        const x = pointA.x + dx / distance * i * segmentLength;
        const y = pointA.y + dy / distance * i * segmentLength + amplitude * Math.sin(i * 0.5 * frequency);
        context.lineTo(x, y);
        context.lineTo(pointB.x, pointB.y);
    }
    context.stroke();
    context.closePath()
}


// Create particles and springs from 
// traced points
for (let i = 0; i < tracedPoints.length; i++) {
    const point = tracedPoints[i];
    const particle = {
        x: point[0],
        y: point[1],
        vx: 0,
        vy: 0
    };
    particles.push(particle);

    for (let j = i + 1; j < tracedPoints.length; j++) {
        const otherPoint = tracedPoints[j];
        const dx = otherPoint[0] - point[0];
        const dy = otherPoint[1] - point[1];
        const distance = Math.sqrt(dx * dx + dy * dy);
        if (distance < springLength) {
            springs.push({
                particleA: particle,
                particleB: particles[j],
                length: distance,
                k: springK
            });
        }
    }
}
function animate() {
    ctx.clearRect(0, 0, width, height);

    // Draw particles
    for (let i = 0; i < particles.length; i++) {
        const particle = particles[i];
        ctx.beginPath();
        ctx.arc(particle.x, particle.y, particleRadius, 0, 2 * Math.PI);
        ctx.fillStyle = "white";
        ctx.fill();
    }

    // Apply forces to springs
    if (springs.length > 0) {
        for (let i = 0; i < springs.length; i++) {
            const spring = springs[i];
            if (spring.particleA && spring.particleB) {
                drawSpring(ctx, spring.particleA, spring.particleB, spring.k, 0); // call drawSpring with correct arguments
            }
        }
    } else {
        for (const spring of springs) {
            drawSpring(ctx, spring.particleA, spring.particleB, spring.k, 0); // call drawSpring with correct arguments
        }
    }

    // request animation frame
    requestAnimationFrame(animate);
}


animate()
```
Posted
Updated 7-May-23 10:12am
v2
Comments
Member 14930137 7-May-23 16:11pm    
1)There is no display on the HTML5Canvas.
2) animate not animation
3) No errors and No Output to stderr,strout when console.log is used.
Member 14930137 7-May-23 16:58pm    
https://www.youtube.com/watch?v=kyQP4t_wOGI is the url for the project.It explains clearly how the spring mass models works and how to implement it.
Member 14930137 7-May-23 17:04pm    
The boilerplate and source code is at:
https://github.com/okpalan/steal-typescript-boilerplate.
https://github.com/okpalan/squishy-text.
Member 14930137 7-May-23 17:37pm    
In addition,i am touch device ,and need an implementation to trackTouch
to provided tension a tension force on the screen
[no name] 9-May-23 8:31am    
The idea is to "start small" and understand what you're doing along each step. A major success would be to show "some output" and build from there (since currently, you say there is "no output")

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900