Feet&Yard to meter converter
Jan 21, 2025 pm 12:10 PMLet's build a simple Feet & Yards to Meters converter web app using Rust and WebAssembly! This guide provides a streamlined approach.
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
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"]
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 }
Build the WebAssembly module:
wasm-pack build --target web
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>
Start a local web server (e.g., using Python's http.server
):
python -m http.server
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Adding Box Shadows to WordPress Blocks and Elements

Create a JavaScript Contact Form With the Smart Forms Framework

Create an Inline Text Editor With the contentEditable Attribute

Making Your First Custom Svelte Transition

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

File Upload With Multer in Node.js and Express
