0

I'm developing a Javascript animation using matter.js. Here is the link:

https://codepen.io/Sergio-Escalona/pen/VwNrwbw

document.addEventListener("DOMContentLoaded", function () {
    // Basic setup
    let Engine = Matter.Engine,
        World = Matter.World,
        Bodies = Matter.Bodies,
        Body = Matter.Body;

    let engine = Engine.create();
    engine.world.gravity.y = 0; // No gravity for a space-like effect

    let render = Matter.Render.create({
        element: document.body,
        engine: engine,
        options: {
            width: window.innerWidth,
            height: window.innerHeight,
            background: 'transparent',
            wireframes: false // for sprite rendering
        }
    });

    // Create static walls
    let thickness = 50; // Ensures ducks don't escape through fast movements
    let wallOptions = { isStatic: true, restitution: 1.0 }; // Perfectly elastic collisions
    let ground = Bodies.rectangle(window.innerWidth / 2, window.innerHeight + thickness / 2, window.innerWidth, thickness, wallOptions);
    let leftWall = Bodies.rectangle(-thickness / 2, window.innerHeight / 2, thickness, window.innerHeight, wallOptions);
    let rightWall = Bodies.rectangle(window.innerWidth + thickness / 2, window.innerHeight / 2, thickness, window.innerHeight, wallOptions);
    let topWall = Bodies.rectangle(window.innerWidth / 2, -thickness / 2, window.innerWidth, thickness, wallOptions);

    World.add(engine.world, [ground, leftWall, rightWall, topWall]);

    // Function to add a new duck with random selection for black duck
    function addDuck() {
        let safeMargin = 100; // A margin to avoid spawning too close to the edge
        let radius = 25;
        // Randomly choose the texture for the duck
        let textureUrl = Math.random() < 0.9 ? 
            'https://assets.codepen.io/12210161/duckling-svgrepo-com.svg' : 
            'https://assets.codepen.io/12210161/duckling-svgrepo-com-bk.svg';
        let duck = Bodies.circle(Math.random() * (window.innerWidth - 2 * safeMargin) + safeMargin, Math.random() * (window.innerHeight - 2 * safeMargin) + safeMargin, radius, {
            restitution: 0.9,
            friction: 0.0,
            frictionAir: 0.0,
            density: 0.001,
            render: {
                sprite: {
                    texture: textureUrl,
                    xScale: 0.1,
                    yScale: 0.1
                }
            }
        });

        // Apply an initial force to ensure movement
        Body.applyForce(duck, {x: duck.position.x, y: duck.position.y}, {x: (Math.random() - 0.5) * 0.05, y: (Math.random() - 0.5) * 0.05});
        World.add(engine.world, duck);
    }

    document.getElementById("oneMoreBtn").addEventListener("click", addDuck);

    // Start the engine and renderer
    Engine.run(engine);
    Matter.Render.run(render);

    // Initially add a duck to start with
    addDuck();
});

I don't know why black ducks are smaller than the regular ones. The svg files dimensions (px) are exactly the same and I can't find where is the problem.

Can anyone help me to solve it?

Thanks in advance!!!

I've checked the svg files and both are exactly the same size. About the Javascript code I can't find anything beyond the render scale but I think it shouldn't be the problem.

1 Answer 1

0

If you open the SVG files in your browser tabs, you will notice that they are not the same. I'm not talking about the size or the colour, but the coordinates - the black duck is centered, unlike the yellow / orange duck, and this is causing the problems with the black duck display.

What you need to do is the following:

  1. Open both of your SVG files in a text editor of your choice
  2. Copy the styling of your duckling-svgrepo-com-bk.svg into a separate file
  3. Replace the entire contents of your duckling-svgrepo-com-bk.svg with the contents of duckling-svgrepo-com.svg, so that the black duck is now identical to the yellow / orange duck
  4. Remove the inline styling for the path and circle elements of your duckling-svgrepo-com-bk.svg, and instead apply the st classes you copied to an empty file in step 2
  5. Copy back the styles from step 3, and save your duckling-svgrepo-com-bk.svg file

What this achieves is that the black duck svg is now exactly the same (sans the colour) as the yellow / orange duck, and your code will work now.

This should be the end result for your black duck's SVG:

<?xml version="1.0" encoding="windows-1252"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg height="800px" width="800px" version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 49 49" xml:space="preserve">
<style type="text/css">
    .st0{fill:#D77328;}
    .st1{fill:#FFFFFF;}
    .st2{fill:#2C2F38;}
    .st3{stroke:#FFFFFF;stroke-miterlimit:10;}
</style>
<g>
<path d="M37.19,26.375c-0.999-1.983-2.486-3.818-4.687-5.119c-0.231-0.136-0.295-0.436-0.123-0.641   c1.116-1.332,4.669-5.868,4.669-9.615c0-6.075-4.925-11-11-11s-11,4.925-11,11c0,6,5.008,9.048,5.873,9.88   c0.086,0.082,0.13,0.186,0.128,0.305c-0.012,1.015-0.342,5.794-5.532,3.985c-1.398-0.487-2.64-1.341-3.686-2.387L5.888,16.84   c-0.469-0.469-1.239-0.409-1.634,0.125C2.92,18.768,0.431,23.017,1.048,29c0,0,0.366,16.187,11.604,19.514   C13.861,48.872,15.126,49,16.386,49h6.858c2.854,0,5.645-0.829,8.027-2.402c0.083-0.055,0.166-0.11,0.25-0.166   C38.085,42.017,40.75,33.439,37.19,26.375z"/>
<path class="st0" d="M47.538,10h-8.206h-2.335c0.03,0.33,0.05,0.662,0.05,1c0,2.351-1.398,5.011-2.716,6.997V18h5.871   c0.926,0,1.854-0.218,2.632-0.721c2.568-1.658,4.434-5.064,5.161-6.554C48.161,10.389,47.912,10,47.538,10z"/>
<circle class="st1" cx="28.048" cy="9" r="4"/>
<circle class="st2" cx="30.048" cy="10" r="2"/>
<path class="st3" d="M20.515,29.887c6.723-3.413,7.533,4.125,7.533,4.125c0,8.75-7,8-7,8   c-5.947,0-8.933-6.269-9.758-8.343c-0.138-0.346,0.071-0.727,0.434-0.808l0.377-0.084C15.006,32.132,17.863,31.233,20.515,29.887z"/>
</g>
</svg>

Of course, you could also adjust your Javascript code to cover the cases when a SVG file is not centered, but this was the easier route to take.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for your help. I noticed that the problem came from Adobe Illustrator edition. Now it is solved!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.