Providing interactive Plotly 3D surface with slider
Creating a self-contained HTML snippet that uses a range input slider to update a 3D WebGL surface plot in real-time via Plotly.react(), smoothly adjusting a frequency parameter on the sinc function without page reloads.
Below is a complete HTML/JavaScript example that uses Plotly’s WebGL-based 3D surface plot and a simple slider to control a parameter in real time. Moving the slider updates the mesh client-side (via Plotly.react) without reloading the page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Interactive 3D Surface with Slider</title>
<!-- Load Plotly from CDN -->
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0; padding: 0;
}
#plot {
width: 100vw;
height: 80vh;
}
#controls {
margin: 1rem;
}
</style>
</head>
<body>
<div id="plot"></div>
<div id="controls">
<label for="freqSlider">
Frequency: <span id="freqValue">1.00</span>
</label>
<input
type="range"
id="freqSlider"
min="0.2" max="5" step="0.05" value="1"
style="width: 300px; vertical-align: middle;"
/>
</div>
<script>
// Resolution of the grid
const GRID_SIZE = 60;
// Build X and Y vectors
function buildAxis(min, max, count) {
const arr = [];
for (let i = 0; i < count; i++) {
arr.push(min + (max - min) * i / (count - 1));
}
return arr;
}
// Calculate the Z values for a given frequency
// Example function: z = sin(freq * r) / r, where r = sqrt(x^2 + y^2)
// Note: Use r=1 when r=0 to avoid division by zero; continuity keeps it finite
function generateSurface(freq) {
const x = buildAxis(-8, 8, GRID_SIZE);
const y = buildAxis(-8, 8, GRID_SIZE);
const z = [];
for (let i = 0; i < GRID_SIZE; i++) {
z[i] = [];
for (let j = 0; j < GRID_SIZE; j++) {
const r = Math.sqrt(x[j]*x[j] + y[i]*y[i]);
z[i][j] = Math.sin(freq * r) / (r === 0 ? 1 : r);
}
}
return { x, y, z };
}
// Initial frequency
const freqSlider = document.getElementById('freqSlider');
const freqValue = document.getElementById('freqValue');
let freq = parseFloat(freqSlider.value);
// Generate initial surface
let surface = generateSurface(freq);
// Define trace and layout
const trace = {
type: 'surface',
x: surface.x,
y: surface.y,
z: surface.z,
colorscale: 'Viridis',
contours: {
z: { show: true, usecolormap: true, project: { z: true } }
}
};
const layout = {
title: 'Real-Time 3D Surface (WebGL)',
autosize: true,
scene: {
xaxis: { title: 'X' },
yaxis: { title: 'Y' },
zaxis: { title: 'Z' }
}
};
// Create the initial plot
Plotly.newPlot('plot', [trace], layout);
// Update function for slider
freqSlider.addEventListener('input', () => {
freq = parseFloat(freqSlider.value);
freqValue.textContent = freq.toFixed(2);
const newSurface = generateSurface(freq);
Plotly.react('plot', [{
type: 'surface',
x: newSurface.x,
y: newSurface.y,
z: newSurface.z,
colorscale: 'Viridis',
contours: { z: { show: true, usecolormap: true, project: { z: true } } }
}], layout);
});
</script>
</body>
</html>
How it works:
- The JavaScript builds a grid (
x, y) and computes z for a given parameter freq.
- Plotly renders a 3D surface using WebGL.
- The slider updates
freq in real time, recalculates the surface, and calls Plotly.react to update the mesh without reloading the page.