Home Web Front-end CSS Tutorial Feet&Yard to meter converter

Feet&Yard to meter converter

Jan 21, 2025 pm 12:10 PM

Let's build a simple Feet & Yards to Meters converter web app using Rust and WebAssembly! This guide provides a streamlined approach.

Feet&Yard to meter converter

First, ensure Rust is installed on your system. Download and install it from https://www.php.cn/link/29f81692d9af87c8826aafca8ff5dad3.

Next, create a new Rust project using Cargo:

cargo new yardandfeetconverter
Copy after login

Modify the Cargo.toml file to include the wasm-bindgen dependency and specify the crate type:

[dependencies]
wasm-bindgen = "0.2"

[lib]
crate-type = ["cdylib"]
Copy after login

Navigate to the src directory and rename main.rs to lib.rs. Replace the contents of lib.rs with this conversion logic:

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn yards_to_meters(yards: f64) -> f64 {
    yards * 0.9144
}

#[wasm_bindgen]
pub fn feet_to_meters(feet: f64) -> f64 {
    feet * 0.3048
}
Copy after login

Build the WebAssembly module:

wasm-pack build --target web
Copy after login

This generates the necessary web files. Now, create an index.html file in the yardandfeetconverter directory. Paste the following code into index.html:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Yard and Feet to Meters Converter</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
      background: linear-gradient(to right, #4facfe, #00f2fe);
    }
    .container {
      text-align: center;
      background: white;
      padding: 2rem;
      border-radius: 10px;
      box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);
    }
    input, select, button {
      margin: 10px;
      padding: 10px;
      font-size: 16px;
      border: 1px solid #ccc;
      border-radius: 5px;
    }
    button {
      background-color: #4facfe;
      color: white;
      cursor: pointer;
    }
    button:hover {
      background-color: #00c4cc;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>Feet & Yards to Meters Converter</h1>
    <input type="number" id="value" placeholder="Enter value">
    <select id="unit">
      <option value="yards">Yards</option>
      <option value="feet">Feet</option>
    </select>
    <button id="convert">Convert</button>
    <p id="result"></p>
  </div>
  <script type="module">
    import init, { yards_to_meters, feet_to_meters } from "./pkg/yardandfeetconverter.js";

    async function setup() {
      await init();
      document.getElementById('convert').addEventListener('click', () => {
        const value = parseFloat(document.getElementById('value').value);
        const type = document.getElementById('unit').value;
        let result;
        if (type === 'yards') {
          result = yards_to_meters(value);
        } else {
          result = feet_to_meters(value);
        }
        document.getElementById('result').innerText = `Result: ${result.toFixed(4)} meters`;
      });
    }
    setup();
  </script>
</body>
</html>
Copy after login

Start a local web server (e.g., using Python's http.server):

python -m http.server
Copy after login

Open your browser and navigate to localhost:8000. You can now convert feet and yards to meters!

The above is the detailed content of Feet&Yard to meter converter. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Adding Box Shadows to WordPress Blocks and Elements Adding Box Shadows to WordPress Blocks and Elements Mar 09, 2025 pm 12:53 PM

Adding Box Shadows to WordPress Blocks and Elements

Create a JavaScript Contact Form With the Smart Forms Framework Create a JavaScript Contact Form With the Smart Forms Framework Mar 07, 2025 am 11:33 AM

Create a JavaScript Contact Form With the Smart Forms Framework

Create an Inline Text Editor With the contentEditable Attribute Create an Inline Text Editor With the contentEditable Attribute Mar 02, 2025 am 09:03 AM

Create an Inline Text Editor With the contentEditable Attribute

Making Your First Custom Svelte Transition Making Your First Custom Svelte Transition Mar 15, 2025 am 11:08 AM

Making Your First Custom Svelte Transition

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

Working With GraphQL Caching

Comparing the 5 Best PHP Form Builders (And 3 Free Scripts) Comparing the 5 Best PHP Form Builders (And 3 Free Scripts) Mar 04, 2025 am 10:22 AM

Comparing the 5 Best PHP Form Builders (And 3 Free Scripts)

File Upload With Multer in Node.js and Express File Upload With Multer in Node.js and Express Mar 02, 2025 am 09:15 AM

File Upload With Multer in Node.js and Express

Show, Don't Tell Show, Don't Tell Mar 16, 2025 am 11:49 AM

Show, Don't Tell

See all articles