Pen Settings

HTML

CSS

CSS Base

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

Vue

              
                <template>
  <div>
    <!-- What's a bad miracle? -->
  </div>
</template>

<script>
import Matter from "https://cdn.skypack.dev/matter-js@0.18.0";
  
export default{
  data() {
    return {
      flagCount: 15,
      flagHeight: 10,
      flagWidth: 20
    }
  },
  methods: {
    initializeMatter() {
      // Initialize engine
      let engine = Matter.Engine.create()
      
      // Create renderer
      let render = Matter.Render.create({
        element: document.body,
        engine: engine,
        options: {
          background: 'transparent',          
          height: window.innerHeight,
          pixelRatio: 1,
          showAngleIndicator: false,
          width: window.innerWidth,
          wireframes: false
        }
      })
      
      // Initialize collision categories
      let cloudCategory = 0x0001
      let stringCategory = 0x0002
      
      // Create cloud
      let cloud = Matter.Bodies.rectangle(window.innerWidth / 2, window.innerHeight / 2, 400, 200, {
        isStatic: true,
        collisionFilter: {
          mask: cloudCategory
        },
        render: {
          sprite: {
            texture: 'https://assets.codepen.io/141041/nope-cloud.png'
          }
        }
      })
      
      // Initialize string of flags
      let string = Matter.Composite.create({})
      
      // Loop until flag count is met
      for (let i = 0; i < this.flagCount; i++) {
        // Get flag position on string
        let x = cloud.position.x
        let y = cloud.position.y + (i * this.flagHeight * 0.4)
        
        // Get flag size
        let height = this.flagHeight
        let width  = this.flagWidth
        
        // Sample random flag color
        let color = _.sample(['red', 'blue', 'lime', 'yellow'])
        
        // Initialize flag options
        let options = {
          friction: 1,
          frictionAir: 0.1,
          mass: 0.1,
          collisionFilter: {
            mask: stringCategory
          },
          render: {
            sprite: {
              texture: `https://assets.codepen.io/141041/flag%40${color}.png`
            }
          }
        }
        
        // Adjust opacity of first three flags
        if (i == 0) { options.render.opacity = 0 }        
        if (i == 1) { options.render.opacity = 0.25 }
        if (i == 2) { options.render.opacity = 0.5 }
        
        // If last flag
        if (i == this.flagCount - 1) {
          // Place further down
          y += this.flagHeight * 4
          
          // Make heavier
          options.mass = 0.5
          
          // And hide it
          options.render = {
            visible: false
          }
          
        }
        
        // Add flag to string
        Matter.Composite.add(string, Matter.Bodies.rectangle(x, y, height, width, options))
        
      }
      
      // Loop through all flags on string
      string.bodies.forEach((body, i) => {
        // Initialize constraint stiffness
        let stiff = 0.2
        
        // Initialize string render options
        let r = {
          anchors: false,
          lineWidth: 1,
          strokeStyle: 'rgba(255, 255, 255, 0.5)',
          type: 'line'
        }
        
        // Reduce stroke opacity for first flags
        if (i < 3) {
          r.strokeStyle = `rgba(255, 255, 255, 0.1)`
        }
        
        // If first flag
        if (i == 0) {
          // Attach it to cloud
          Matter.Composite.add(string, Matter.Constraint.create({
            bodyA: cloud,
            pointA: {
              x: 0,
              y: 0
            },
            bodyB: body,
            pointB: {
              x: 0,
              y: 0
            },
            render: r,
            stiffness: stiff
          }))
          
        }
        
        // If not the last flag
        if (i != string.bodies.length - 1) {
          // Attach top and bottom corner of flag
          Matter.Composite.add(string, Matter.Constraint.create({
            bodyA: body,
            pointA: {
              x: 0,
              y: -(this.flagHeight / 2)
            },
            bodyB: body,
            pointB: {
              x: 0,
              y: (this.flagHeight / 2)
            },
            render: r,
            stiffness: stiff
          }))
        }
        
        // If less than the last flag
        if (i < string.bodies.length - 1) {
          // Attach current flag to next flag
          Matter.Composite.add(string, Matter.Constraint.create({
            bodyA: body,
            pointA: {
              x: 0,
              y: (this.flagHeight / 2)
            },
            bodyB: string.bodies[i + 1],
            pointB: {
              x: 0,
              y: -(this.flagHeight / 2)
            },
            render: r,
            stiffness: stiff
          }))
        }
        
      })
      
      // Add bodies to world
      Matter.Composite.add(engine.world, [cloud, string])
      
      // Create mouse
      let mouse = Matter.Mouse.create(render.canvas)
      
      // Add mouse constraint
      let mouseConstraint = Matter.MouseConstraint.create(engine, {
        mouse: mouse,
        constraint: {
          render: {
            visible: false
          }
        }
      })
      
      // Add mouse constaint to world
      Matter.Composite.add(engine.world, [mouseConstraint])
      
      // Set mouse
      render.mouse = mouse
      
      // Run renderer
      Matter.Render.run(render)
      
      // Create runner
      let runner = Matter.Runner.create()
      
      // Run the engine
      Matter.Runner.run(runner, engine)
      
      // Before update event
      Matter.Events.on(engine, 'beforeUpdate', () => {
        // Update cloud position
        Matter.Body.setPosition(cloud, {
          x: mouse.position.x,
          y: mouse.position.y
        })
        
      })
      
      // Listen for resize
      window.addEventListener('resize', () => { 
        // Resize renderer
        render.bounds.max.x = window.innerWidth
        render.bounds.max.y = window.innerHeight
        render.options.width = window.innerWidth
        render.options.height = window.innerHeight
        render.canvas.width = window.innerWidth
        render.canvas.height = window.innerHeight
        
      })
            
    }
  },
  mounted() {
    // Initialize matter
    this.initializeMatter()
    
  }
}
</script>

<style>
  html, body{
    height: 100%;
    width: 100%;
  }
  
  body{
    background-color: black;
    background-image: url(https://assets.codepen.io/141041/nope-background.jpg);
    background-repeat: no-repeat;
    background-position: center center;
    background-size: cover;
  }
  
  canvas{
    cursor: none;
  }
</style>

              
            
!
999px

Console