<jupyter_start><jupyter_text>FittingThe `element.fit()` method determines a bounding box in X/Ycoordinates that contains all visible elements and adjusts thescaling and translation for the HTML canvas element to makethe bounding box visible.  You can use `fit` to "just draw stuff somewhere" and let the `fit` methodfigure out how to put them in the visible region.**Warning:**  A buggy program may draw elements too far apart and inthis case the resultant `fit` may look blank because the drawnelements are too small to see inside the huge bounding box thatfit infers.<jupyter_code>from jp_doodle import dual_canvas
from IPython.display import display
# In this demonstration we do most of the work in Javascript.

# Draw a spiral using trigonometric functions and use fit to
# automatically make the whole spiral visible with a boundary.

demo1 = dual_canvas.DualCanvasWidget(width=320, height=220)
display(demo1)

demo1.js_init("""

var points = []
var dr = 10;
var dtheta = 0.2;
for (var i=0; i<130; i++) {
    var r = dr * i;
    var theta = dtheta * i;
    points.push( [Math.sin(theta) * r, Math.cos(theta) * r])
}
element.polygon({
    points: points, color: "green", fill: false, lineWidth:25, close: false
})

// Fit the figure into the available space with a 50 units margin.
var stats = element.fit(null, 50);

element.print("Canvas limits are", stats.min_x, stats.min_y, stats.max_x, stats.max_y);
""")
# Python analogue
import math

demo2 = dual_canvas.DualCanvasWidget(width=320, height=220)
display(demo2)

points = []
dr = 10
dtheta = 0.2;
for i in range(130):
    r = dr * i;
    theta = dtheta * i;
    points.append( [math.sin(theta) * r, math.cos(theta) * r])
demo2.element.polygon(dict(
    points=points, color="green", fill=False, lineWidth=35, close=False
))

demo2.fit(margin=100)
# The first argument to fit, if provided, specifies the max/min values to fit.
# The following call changes the fit for demo2 above to zoom in a bit.
demo2.fit(dict(min_x=-500, max_x=500, min_y=-200, max_y=800))
#demo2.save_pixels_to_png_async("Fit.png")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10616929698708752, 'fraction_numerical': 0.028694404591104734, 'mean_word_length': 4.638814016172507, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '367940', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 724, 'n_tokens_neox': 669, 'n_words': 253, 'path': 'notebooks/Feature demonstrations/The fit method.ipynb', 'repo_name': 'lingruiluo/jp_doodle', 'size': '2091'}