Mandelbrot mit JSXGraph
Aus Wiki1
(Unterschied zwischen Versionen)
| (Der Versionsvergleich bezieht 36 dazwischenliegende Versionen mit ein.) | |||
| Zeile 1: | Zeile 1: | ||
| - | <jsxgraph box="jxgbox" width=" | + | <jsxgraph box="jxgbox" width="600" height="700"> |
| - | + | const board = JXG.JSXGraph.initBoard('jxgbox', { | |
boundingbox: [-2.5, 1.5, 1.5, -1.5], | boundingbox: [-2.5, 1.5, 1.5, -1.5], | ||
| - | axis: | + | axis: false, |
showNavigation: false, | showNavigation: false, | ||
showCopyright: false | showCopyright: false | ||
| Zeile 9: | Zeile 9: | ||
// Slider für Iterationstiefe | // Slider für Iterationstiefe | ||
| - | const sliderIter = board.create('slider', [[-2.3, 1.3], [ | + | const sliderIter = board.create('slider', [[-2.3, 1.3], [0.5, 1.3], [10, 50, 300]], { |
name:'Tiefe', | name:'Tiefe', | ||
snapWidth:1 | snapWidth:1 | ||
| Zeile 15: | Zeile 15: | ||
// Slider für Zoom | // Slider für Zoom | ||
| - | const sliderZoom = board.create('slider', [[-2.3, 1. | + | const sliderZoom = board.create('slider', [[-2.3, 1.2], [0.5, 1.2], [1, 1, 50]], { |
name:'Zoom', | name:'Zoom', | ||
snapWidth:1 | snapWidth:1 | ||
}); | }); | ||
| + | |||
// Canvas ins DOM hängen | // Canvas ins DOM hängen | ||
const div = document.getElementById('jxgbox'); | const div = document.getElementById('jxgbox'); | ||
const canvas = document.createElement('canvas'); | const canvas = document.createElement('canvas'); | ||
| - | canvas.width = | + | canvas.width = 600; |
| - | canvas.height = | + | canvas.height = 600; |
canvas.style.position = 'absolute'; | canvas.style.position = 'absolute'; | ||
| - | canvas.style.left = " | + | canvas.style.left = "0px"; |
| - | canvas.style.top = " | + | canvas.style.top = "150px"; |
div.appendChild(canvas); | div.appendChild(canvas); | ||
const ctx = canvas.getContext('2d'); | const ctx = canvas.getContext('2d'); | ||
| Zeile 42: | Zeile 43: | ||
return iter; | return iter; | ||
} | } | ||
| + | |||
| + | // Anfangsbereich | ||
| + | let centerX = -0.5; | ||
| + | let centerY = 0; | ||
| + | let baseWidth = 3.5; | ||
| + | let baseHeight = 3.0; | ||
function drawMandelbrot(maxIter, zoom) { | function drawMandelbrot(maxIter, zoom) { | ||
| - | const | + | const spanX = baseWidth / zoom; |
| + | const spanY = baseHeight / zoom; | ||
| - | + | const xMin = centerX - spanX/2; | |
| - | const spanX | + | const xMax = centerX + spanX/2; |
| - | const | + | const yMin = centerY - spanY/2; |
| - | + | const yMax = centerY + spanY/2; | |
| - | const yMin = -spanY/2 | + | |
| + | const img = ctx.createImageData(canvas.width, canvas.height); | ||
for (let px = 0; px < canvas.width; px++) { | for (let px = 0; px < canvas.width; px++) { | ||
| Zeile 67: | Zeile 76: | ||
} else { | } else { | ||
const c = Math.floor(255 * iter / maxIter); | const c = Math.floor(255 * iter / maxIter); | ||
| - | img.data[idx] = c; | + | img.data[idx] = c; |
| - | img.data[idx+1] = 100; | + | img.data[idx+1] = 100; |
| - | img.data[idx+2] = 255 - c; | + | img.data[idx+2] = 255 - c; |
| - | img.data[idx+3] = 255; | + | img.data[idx+3] = 255; |
} | } | ||
} | } | ||
| Zeile 83: | Zeile 92: | ||
sliderIter.on('drag', update); | sliderIter.on('drag', update); | ||
sliderZoom.on('drag', update); | sliderZoom.on('drag', update); | ||
| + | |||
| + | |||
| + | // Panning mit Maus | ||
| + | let isDragging = false; | ||
| + | let lastX, lastY; | ||
| + | |||
| + | canvas.addEventListener('mousedown', e => { | ||
| + | isDragging = true; | ||
| + | lastX = e.offsetX; | ||
| + | lastY = e.offsetY; | ||
| + | }); | ||
| + | |||
| + | canvas.addEventListener('mouseup', () => { | ||
| + | isDragging = false; | ||
| + | }); | ||
| + | |||
| + | canvas.addEventListener('mouseleave', () => { | ||
| + | isDragging = false; | ||
| + | }); | ||
| + | |||
| + | canvas.addEventListener('mousemove', e => { | ||
| + | if (isDragging) { | ||
| + | const dx = e.offsetX - lastX; | ||
| + | const dy = e.offsetY - lastY; | ||
| + | lastX = e.offsetX; | ||
| + | lastY = e.offsetY; | ||
| + | |||
| + | // Verschiebung in komplexer Ebene berechnen | ||
| + | const zoom = Math.round(sliderZoom.Value()); | ||
| + | const spanX = baseWidth / zoom; | ||
| + | const spanY = baseHeight / zoom; | ||
| + | |||
| + | centerX -= dx / canvas.width * spanX; | ||
| + | centerY -= dy / canvas.height * spanY; | ||
| + | |||
| + | update(); | ||
| + | } | ||
| + | }); | ||
// Initial zeichnen | // Initial zeichnen | ||
update(); | update(); | ||
| - | |||
</jsxgraph> | </jsxgraph> | ||
| + | |||
| + | Mit den Schiebereglern können Iterations-Tiefe und Zoom gesteuert werden. Mit der Maus+linke Maustaste kann der Bildausschnitt verschoben werden. | ||
| + | |||
| + | |||
| + | [[Kategorie:KI-generiert]] | ||
Aktuelle Version vom 14:08, 22. Dez. 2025
Mit den Schiebereglern können Iterations-Tiefe und Zoom gesteuert werden. Mit der Maus+linke Maustaste kann der Bildausschnitt verschoben werden.
