Home >Web Front-end >CSS Tutorial >Feet&Yard to meter converter
Let'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:
<code class="language-bash">cargo new yardandfeetconverter</code>
Modify the Cargo.toml
file to include the wasm-bindgen
dependency and specify the crate type:
<code class="language-toml">[dependencies] wasm-bindgen = "0.2" [lib] crate-type = ["cdylib"]</code>
Navigate to the src
directory and rename main.rs
to lib.rs
. Replace the contents of lib.rs
with this conversion logic:
<code class="language-rust">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 }</code>
Build the WebAssembly module:
<code class="language-bash">wasm-pack build --target web</code>
This generates the necessary web files. Now, create an index.html
file in the yardandfeetconverter
directory. Paste the following code into index.html
:
<code class="language-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></code>
Start a local web server (e.g., using Python's http.server
):
<code class="language-bash">python -m http.server</code>
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!