json-example.js

total 0
used 0
limit 0
/* title: JSON Save Restore categories: json files: ../point_src/core/head.js ../point_src/pointpen.js ../point_src/pointdraw.js ../point_src/math.js ../point_src/extras.js ../point_src/point-content.js ../point_src/pointlist.js ../point_src/pointlistpen.js ../point_src/point.js ../point_src/stage.js mouse dragging stroke --- We can convert a `Point` or `PointList` to JSON. Any pointlist will cast and parse from a string: const strPoints = points.toJSON() `[ [250,150,10,270], [450,520,8,270] ]` points.fromJSON(stringPoints) In this example, any change is written to `localStorage`. Refreshing the page will restore the previous changes. A JSON dump of `stage.points` exists in `localStorage["polypoint-json-example"]` */ class MainStage extends Stage { // canvas = document.getElementById('playspace'); canvas = 'playspace' mounted(){ this.restoreJSON() if(this.points == undefined) { this.points = new PointList( new Point({ x: 250, y: 150 , radius: 10 , vx: 1, vy: 0 , mass: 2 }) , new Point({ x: 400, y: 320 , vx: -1, vy: 0 , radius: 20 , mass: 10 }) , new Point({ x: 200, y: 320 , vx: .4, vy: -.1 , radius: 10 , mass: 8 }) , new Point({ x: 110, y: 220 , vx: .4, vy: -.1 , radius: 8 , mass: 8 }) , new Point({ x: 230, y: 120 , vx: .4, vy: -.1 , radius: 22 , mass: 8 }) , new Point({ x: 450, y: 520 , vx: .4, vy: -.1 , radius: 8 , mass: 8 }) ) } this.dragging.add(...this.points) let d = this.saveButton = { label: "Save JSON" , onclick: (ev, proxySelf) =>{ console.log('Save') this.saveJSON() } } addButton('save', d) addButton('restore', { label: "Restore JSON" , onclick: (ev, proxySelf) =>{ console.log('restore') this.restoreJSON() } }) } draw(ctx){ this.clear(ctx) this.points.pen.indicators(ctx) } restoreJSON() { let d = localStorage['polypoint-json-example'] if(d) { let loadedPoints = PointList.fromJSON(d) this.points = loadedPoints this.dragging.set(...this.points) } } onMouseup(ev) { // clearTimeout(this._saveTimer) // this._saveTimer = setTimeout(this.saveJSON.bind(this), 1000) } saveJSON() { /* create the JSON of the polypoints and store to localstorage */ let text = this.points.toJSON() let key = 'polypoint-json-example'; localStorage[key] = text console.log(text.length, 'chars written to', key) return text } } Polypoint.head.installFunctions('Point', { toJSON() { /* By default the Point.toJSON return the array value. */ return this.asArray() } }) Polypoint.head.installFunctions('PointList', { toJSON() { // console.log('Store to JSON', this) let output = [] this.forEach((p)=>{ let d = p.toJSON() output.push(d) }) return JSON.stringify(output) } , fromJSON(text) { let output = JSON.parse(text) return PointList.from(output).cast() } }) Polypoint.head.staticFunctions('PointList', { fromJSON(text) { let output = JSON.parse(text) return PointList.from(output).cast() } }) stage = MainStage.go(/*{ loop: true }*/)
Run
Meta Data
title JSON Save Restore
imports ()
files ('../point_src/core/head.js', '../point_src/pointpen.js', '../point_src/pointdraw.js', '../point_src/math.js', '../point_src/extras.js', '../point_src/point-content.js', '../point_src/pointlist.js', '../point_src/pointlistpen.js', '../point_src/point.js', '../point_src/stage.js', 'mouse', 'dragging', 'stroke')
unused_keys ()
unknown_keys ('categories',)
categories ['json']
filepath_exists True
path json-example.js
filepath json-example.js
clean_files ('../point_src/core/head.js', '../point_src/pointpen.js', '../point_src/pointdraw.js', '../point_src/math.js', '../point_src/extras.js', '../point_src/compass.js', '../point_src/center.js', '../point_src/point-content.js', '../point_src/pointlistdraw.js', '../point_src/pointlistgradient.js', '../point_src/pointlistshape.js', '../point_src/pointlistgenerator.js', '../point_src/unpack.js', '../point_src/pointlist.js', '../point_src/pointlistpen.js', '../point_src/relative-xy.js', '../point_src/pointcast.js', '../point_src/point.js', '../point_src/stage-resize.js', '../point_src/functions/resolve.js', '../point_src/stage.js', '../point_src/events.js', '../point_src/automouse.js', '../point_src/functions/clamp.js', '../point_src/distances.js', '../point_src/protractor.js', '../point_src/text/beta.js', '../point_src/dragging.js', '../point_src/setunset.js', '../point_src/stroke.js')
markdown {'html': '<p>We can convert a <code>Point</code> or <code>PointList</code> to JSON.\nAny pointlist will cast and parse from a string:</p>\n<pre><code>const strPoints = points.toJSON()\n`[\n [250,150,10,270],\n [450,520,8,270]\n]`\npoints.fromJSON(stringPoints)\n</code></pre>\n<p>In this example, any change is written to <code>localStorage</code>.\nRefreshing the page will restore the previous changes.\nA JSON dump of <code>stage.points</code> exists in <code>localStorage["polypoint-json-example"]</code></p>', 'content': 'title: JSON Save Restore\ncategories: json\nfiles:\n ../point_src/core/head.js\n ../point_src/pointpen.js\n ../point_src/pointdraw.js\n ../point_src/math.js\n ../point_src/extras.js\n ../point_src/point-content.js\n ../point_src/pointlist.js\n ../point_src/pointlistpen.js\n ../point_src/point.js\n ../point_src/stage.js\n mouse\n dragging\n stroke\n---\n\nWe can convert a `Point` or `PointList` to JSON.\nAny pointlist will cast and parse from a string:\n\n const strPoints = points.toJSON()\n `[\n [250,150,10,270],\n [450,520,8,270]\n ]`\n points.fromJSON(stringPoints)\n\nIn this example, any change is written to `localStorage`.\nRefreshing the page will restore the previous changes.\nA JSON dump of `stage.points` exists in `localStorage["polypoint-json-example"]`'}