Creating 3D extrusions with three.js using image files as shape definitions

How can I replace the hearts with the leaf provided?

I'm working on a web app that involves falling objects using Three.JS. I began with a heart shape, but now I need to swap it out with an autumn leaf (that's the only change needed).

This is the image:

I'm not quite sure how shapes function in this context.

Try it yourself:

'use strict';

var container, stats;
var camera, scene, renderer;
var group,
    shapes = [];
init();

function init() {
  container = document.createElement('div');
  document.body.appendChild(container);
  scene = new THREE.Scene();
  camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000);
  camera.position.set(0, 150, 500);
  scene.add(camera);

  var light = new THREE.DirectionalLight(0x9955ff, 2);
  light.position.x = -500;
  light.position.y = 500;
  camera.add(light);

  var light = new THREE.DirectionalLight(0x9955ff, 1);
  light.position.x = 500;
  light.position.y = -500;
  light.position.z = -150;
  camera.add(light);

  scene.background = new THREE.Color('#993355');

  var x = -25,
      y = -250;
  // Leaf shape code would be placed here

  var extrudeSettings = { amount: 1, bevelEnabled: true, bevelSegments: 20, steps: 2, bevelSize: 20, bevelThickness: 10 };
    /* Loop for positioning and adding leaf shapes would go here */

  renderer = new THREE.WebGLRenderer({ antialias: true });
  renderer.setPixelRatio(window.devicePixelRatio);
  renderer.setSize(window.innerWidth, window.innerHeight);
  container.appendChild(renderer.domElement);

  render();
}
// Remaining functions, such as addShape(), animate(), and render(), remain unchanged
body{
  margin: 0;
  padding: 0;
  overflow: hidden;
}
<script src='https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js'></script>

It needs to be the same, exactly the same. Only the hearts need to be replaced by leaves.

Answer №1

If you need to extrude a shape that is defined by an image file, here's how you can do it:

Start by using an online image converter like to convert your file into SVG format. In some cases, you may have to manually edit the generated SVG file to remove any unnecessary elements.

var text = `<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
 "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
 width="800.000000pt" height="800.000000pt" viewBox="0 0 800.000000 800.000000"
 preserveAspectRatio="xMidYMid meet">
<metadata>
Created by potrace 1.15, written by Peter Selinger 2001-2017
</metadata>
<g transform="translate(0.000000,800.000000) scale(0.100000,-0.100000)"
fill="#000000" stroke="none">
<path d="m311 -1104 c9 -47 29 -114 44 -150 53 -126 236 -481 250 -487 8 -3
27 2 41 12 41 27 50 15 57 -77 10 -147 47 -198 142 -198 30 0 44 7 71 36 19
19 38 33 44 29 20 -12 22 -79 5 -182 -27 -165 -5 -311 70 -468 42 -88 66 -104
131 -87 60 16 213 86 529 242 207 102 277 142 342 194 45 36 87 66 93 66 20 0
5 -54 -100 -375 -108 -330 -125 -398 -118 -480 6 -70 27 -95 80 -95 50 0 58
-20 24 -63 -104 -128 -106 -132 -106 -170 0 -53 37 -88 125 -117 76 -24 98
-39 87 -56 -4 -6 -52 -41 -107 -77 -157 -102 -225 -186 -225 -276 0 -53 37
-96 112 -128 32 -14 58 -32 58 -40 0 -25 -103 -62 -343 -122 -313 -79 -327
-87 -267 -152 39 -44 26 -58 -38 -40 -77 20 -522 215 -740 323 -138 69 -206
98 -213 91 -7 -7 -2 -48 15 -130 14 -66 42 -212 63 -324 36 -198 37 -211 38
-436 l0 -233 -30 -29 c-43 -43 -109 -58 -157 -38 -48 20 -81 71 -73 112 3 17
30 71 60 120 30 50 61 110 70 134 17 50 19 165 4 231 -6 25 -35 161 -64 304
-29 142 -58 263 -64 269 -8 8 -27 4 -68 -12 -112 -45 -340 -160 -538 -271
-214 -121 -410 -220 -431 -220 -20 0 -17 10 16 59 63 93 50 98 -280 104 -228
...
shapes.
var geometry = new THREE.ExtrudeBufferGeometry( shapes, extrusionSettings );
geometry.center();

var mesh = new THREE.Mesh( geometry, material );
mesh.scale.y *= - 1;

scene.add( mesh );

You will need to include THREE.SVGLoader in your project to parse the file and extrude it.

SVGLoader can be found in the threejs/examples/jsm/loaders/ directory and should be included in your project for this process to work correctly.

https://i.sstatic.net/HQ8Ze.png

Using three.js r153

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Looking to personalize your jVectorMap region popups? Here's how!

Currently, I am working with the jVectorMap library. While hovering over each country, the default label displays the country name. However, I would like to customize the map so that it shows regions based on database values. How can I achieve this custo ...

Using JSON to create bootstrap styled link buttons

My current code is functioning well with links. However, when I try to use a bootstrap button instead of a regular button, the button appears in the table but no longer directs to the link. var button = "<button class='btn btn-inf ...

Tips for customizing MUI PaperProps using styled components

I am trying to customize the width of the menu using styled components in MUI. Initially, I attempted the following: const StyledMenu = styled(Menu)` && { width: 100%; } `; However, this did not have any effect. After further research, I ...

Retrieve client-side environment variables

When making multiple API calls within my components using the fetch method, I found myself constantly having to determine which environment I was in. To streamline this logic, I created a Utils class that provides the appropriate base URL: export default c ...

Do watches operate asynchronously?

I am monitoring the status of a variable called radioStatus within a Vue instance: watch: { radioStatus: function(val) { if (!this.discovery) { $.ajax({ url: '/switch/api/radio/' + (val ? 'on' : 'off') }) ...

How can JavaScript effectively retrieve the JPEG comment from a JPEG file?

How can one properly retrieve the JPEG comment field (not EXIF, but the COM field) from a JPEG file using JavaScript, particularly when running node on the command line? While there are several libraries available for reading EXIF data in JavaScript, I ha ...

Is there a way to incorporate both max-width and min-width in a matchMedia query effectively?

I'm currently utilizing the matchMedia API in my JavaScript code to identify viewports, with the goal of reducing DOM manipulations. Instead of using display: none extensively, I am opting for a v-if directive from Vue to determine when elements are ...

TinyMCE - Optimal Approach for Saving Changes: keyup vs onChange vs blur

In the context of my Filemaker file, I am utilizing the TinyMCE editor. My goal is to automatically save any changes made by the user, whether it's typing, applying formatting, inserting an image, or making any other modifications. I have a function ...

Is it possible for two node scripts running on the same machine to interfere with each other's execution

In the scenario where a parent node file (such as an express server) spawns child nodes that execute computationally intense tasks, will these children cause blocking for the parent process? ...

How can JavaScript be used to prevent pinch and zoom functionality on Google Maps?

In my project, I am utilizing Google Maps but I want to disable the zooming effect on the map. To achieve this, I have disabled the zoom control using zoomControl: false,. However, this modification only applies to desktops and laptops as they do not suppo ...

Enhancing Vuejs Security: Best Practices and Tips for Secure Development

Recently, I developed a Web Application utilizing Vue.js and fetching data from the backend using 'vue-resource' in combination with Express and Postgres. Now, my main objective is to enhance its security by integrating an API Key. I am somewha ...

Creating a fresh object from a previous one using JavaScript:

I am working towards a goal where I aim to take an object with string values, translate those values, and then create a new object filled with the translated strings. For example, if I start with: const strings = { "name": "my name", "age": "my ag ...

Obtaining solely the words found within HTML documents

In my Python 2.7 project, I have a folder containing multiple HTML pages that I need to extract only words from. My current process involves opening the HTML file, using the Beautiful Soup library to extract text, and then writing it to a new file. However ...

Guide on using a for loop to assign JSON data

I am currently developing a local weather widget for my website and have successfully integrated data using the OpenWeather Api. However, due to my limited experience in jquery/javascript, I am facing challenges in loading data for a 10-day forecast. Any a ...

Can you explain the significance of the error message stating "XMLHttpRequest.timeout cannot be set for synchronous http(s) requests made from the window context"?

I'm experiencing some timeouts with a synchronous XML HTTP request in Safari on my Mac. In an attempt to fix this issue, I added a timeout setting like this: req.open(this.method, fullURL, this.isAsync); req.setRequestHeader('Content-Typ ...

What are the steps to manually activate input validation in Angular 2?

Having two inputs is the scenario here: The first input undergoes custom validator application The second input has a dynamic and editable value utilized in the custom validator If the custom validator is applied on the first input, then focus shifts to ...

Tips for sorting through existing data without resorting to a remote call - Material Table repository by mbrn (mbrn/material

Currently I am using the mbrn/material-table library which includes filtering on a column and remote data feature. However, when I apply a filter term, the table sends an API call to the server with the filter criteria in the query object. My desired outco ...

Tips for uploading a file with fetch

While I know this question has been asked before, none of the solutions seem to be working for me. Initially, I attempted to resolve the issue using axios, but it appears that there is a bug preventing me from utilizing it for file uploads. Therefore, I am ...

How can I pass the dynamically generated ID from PHP to AJAX/jQuery using an anchor tag?

I'm seeking help with jQuery and Ajax as I am new to it. My issue is that I have multiple 'edit' buttons in a table, one for each row's data. When I click on an edit button to modify the data, they all open at once instead of just the s ...

Issue with reactivity in deeply nested objects not functioning as expected

Currently, I am utilizing Konvajs for my project. In simple terms, Konvajs provides a canvas for drawing and editing elements such as text nodes that can be manipulated by dragging and dropping. These nodes are organized within groups, which are then added ...