From c721a7975e9246bedb6a93e6b4b4b28e01abd3f0 Mon Sep 17 00:00:00 2001 From: Andreas Herd Date: Mon, 18 Nov 2024 17:59:25 +0200 Subject: [PATCH] fix tree balance formula --- docs/index.html | 2 +- ...{find-solution-Cjopq3Ht.js => find-solution-C0sgTXPj.js} | 2 +- docs/none/{index-DGsLmoZt.js => index-HpxU7qA1.js} | 2 +- src/layout/find-solution.ts | 6 +++++- src/utils/tree-balance.ts | 3 ++- 5 files changed, 10 insertions(+), 5 deletions(-) rename docs/none/{find-solution-Cjopq3Ht.js => find-solution-C0sgTXPj.js} (89%) rename docs/none/{index-DGsLmoZt.js => index-HpxU7qA1.js} (99%) diff --git a/docs/index.html b/docs/index.html index 8889c0a..8fff6df 100644 --- a/docs/index.html +++ b/docs/index.html @@ -5,7 +5,7 @@ Demo page for diorama - + diff --git a/docs/none/find-solution-Cjopq3Ht.js b/docs/none/find-solution-C0sgTXPj.js similarity index 89% rename from docs/none/find-solution-Cjopq3Ht.js rename to docs/none/find-solution-C0sgTXPj.js index c53d3da..4153b67 100644 --- a/docs/none/find-solution-Cjopq3Ht.js +++ b/docs/none/find-solution-C0sgTXPj.js @@ -2,4 +2,4 @@ var Fe=Object.defineProperty;var Ue=(m,d,v)=>d in m?Fe(m,d,{enumerable:!0,config * @license * Copyright 2019 Google LLC * SPDX-License-Identifier: Apache-2.0 - */const m=Symbol("Comlink.proxy"),d=Symbol("Comlink.endpoint"),v=Symbol("Comlink.releaseProxy"),q=Symbol("Comlink.finalizer"),T=Symbol("Comlink.thrown"),Y=e=>typeof e=="object"&&e!==null||typeof e=="function",Et={canHandle:e=>Y(e)&&e[m],serialize(e){const{port1:t,port2:r}=new MessageChannel;return D(e,t),[r,[r]]},deserialize(e){return e.start(),Tt(e)}},bt={canHandle:e=>Y(e)&&T in e,serialize({value:e}){let t;return e instanceof Error?t={isError:!0,value:{message:e.message,name:e.name,stack:e.stack}}:t={isError:!1,value:e},[t,[]]},deserialize(e){throw e.isError?Object.assign(new Error(e.value.message),e.value):e.value}},$=new Map([["proxy",Et],["throw",bt]]);function jt(e,t){for(const r of e)if(t===r||r==="*"||r instanceof RegExp&&r.test(t))return!0;return!1}function D(e,t=globalThis,r=["*"]){t.addEventListener("message",function n(a){if(!a||!a.data)return;if(!jt(r,a.origin)){console.warn(`Invalid origin '${a.origin}' for comlink proxy`);return}const{id:u,type:i,path:o}=Object.assign({path:[]},a.data),s=(a.data.argumentList||[]).map(w);let c;try{const p=o.slice(0,-1).reduce((h,S)=>h[S],e),y=o.reduce((h,S)=>h[S],e);switch(i){case"GET":c=y;break;case"SET":p[o.slice(-1)[0]]=w(a.data.value),c=!0;break;case"APPLY":c=y.apply(p,s);break;case"CONSTRUCT":{const h=new y(...s);c=kt(h)}break;case"ENDPOINT":{const{port1:h,port2:S}=new MessageChannel;D(e,S),c=Rt(h,[h])}break;case"RELEASE":c=void 0;break;default:return}}catch(p){c={value:p,[T]:0}}Promise.resolve(c).catch(p=>({value:p,[T]:0})).then(p=>{const[y,h]=R(p);t.postMessage(Object.assign(Object.assign({},y),{id:u}),h),i==="RELEASE"&&(t.removeEventListener("message",n),Z(t),q in e&&typeof e[q]=="function"&&e[q]())}).catch(p=>{const[y,h]=R({value:new TypeError("Unserializable return value"),[T]:0});t.postMessage(Object.assign(Object.assign({},y),{id:u}),h)})}),t.start&&t.start()}function It(e){return e.constructor.name==="MessagePort"}function Z(e){It(e)&&e.close()}function Tt(e,t){return F(e,[],t)}function C(e){if(e)throw new Error("Proxy has been released and is not useable")}function J(e){return _(e,{type:"RELEASE"}).then(()=>{Z(e)})}const M=new WeakMap,P="FinalizationRegistry"in globalThis&&new FinalizationRegistry(e=>{const t=(M.get(e)||0)-1;M.set(e,t),t===0&&J(e)});function Ct(e,t){const r=(M.get(t)||0)+1;M.set(t,r),P&&P.register(e,t,e)}function Mt(e){P&&P.unregister(e)}function F(e,t=[],r=function(){}){let n=!1;const a=new Proxy(r,{get(u,i){if(C(n),i===v)return()=>{Mt(a),J(e),n=!0};if(i==="then"){if(t.length===0)return{then:()=>a};const o=_(e,{type:"GET",path:t.map(s=>s.toString())}).then(w);return o.then.bind(o)}return F(e,[...t,i])},set(u,i,o){C(n);const[s,c]=R(o);return _(e,{type:"SET",path:[...t,i].map(p=>p.toString()),value:s},c).then(w)},apply(u,i,o){C(n);const s=t[t.length-1];if(s===d)return _(e,{type:"ENDPOINT"}).then(w);if(s==="bind")return F(e,t.slice(0,-1));const[c,p]=Q(o);return _(e,{type:"APPLY",path:t.map(y=>y.toString()),argumentList:c},p).then(w)},construct(u,i){C(n);const[o,s]=Q(i);return _(e,{type:"CONSTRUCT",path:t.map(c=>c.toString()),argumentList:o},s).then(w)}});return Ct(a,e),a}function Pt(e){return Array.prototype.concat.apply([],e)}function Q(e){const t=e.map(R);return[t.map(r=>r[0]),Pt(t.map(r=>r[1]))]}const K=new WeakMap;function Rt(e,t){return K.set(e,t),e}function kt(e){return Object.assign(e,{[m]:!0})}function R(e){for(const[t,r]of $)if(r.canHandle(e)){const[n,a]=r.serialize(e);return[{type:"HANDLER",name:t,value:n},a]}return[{type:"RAW",value:e},K.get(e)||[]]}function w(e){switch(e.type){case"HANDLER":return $.get(e.name).deserialize(e.value);case"RAW":return e.value}}function _(e,t,r){return new Promise(n=>{const a=Nt();e.addEventListener("message",function u(i){!i.data||!i.data.id||i.data.id!==a||(e.removeEventListener("message",u),n(i.data))}),e.start&&e.start(),e.postMessage(Object.assign({id:a},t),r)})}function Nt(){return new Array(4).fill(0).map(()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16)).join("-")}var zt=function(){return!0};function f(e){return e!=null&&typeof e=="object"&&e["@@functional/placeholder"]===!0}function g(e){return function t(r){return arguments.length===0||f(r)?t:e.apply(this,arguments)}}function l(e){return function t(r,n){switch(arguments.length){case 0:return t;case 1:return f(r)?t:g(function(a){return e(r,a)});default:return f(r)&&f(n)?t:f(r)?g(function(a){return e(a,n)}):f(n)?g(function(a){return e(r,a)}):e(r,n)}}}function E(e,t){switch(e){case 0:return function(){return t.apply(this,arguments)};case 1:return function(r){return t.apply(this,arguments)};case 2:return function(r,n){return t.apply(this,arguments)};case 3:return function(r,n,a){return t.apply(this,arguments)};case 4:return function(r,n,a,u){return t.apply(this,arguments)};case 5:return function(r,n,a,u,i){return t.apply(this,arguments)};case 6:return function(r,n,a,u,i,o){return t.apply(this,arguments)};case 7:return function(r,n,a,u,i,o,s){return t.apply(this,arguments)};case 8:return function(r,n,a,u,i,o,s,c){return t.apply(this,arguments)};case 9:return function(r,n,a,u,i,o,s,c,p){return t.apply(this,arguments)};case 10:return function(r,n,a,u,i,o,s,c,p,y){return t.apply(this,arguments)};default:throw new Error("First argument to _arity must be a non-negative integer no greater than ten")}}function tt(e,t,r){return function(){for(var n=[],a=0,u=e,i=0,o=!1;i=arguments.length)?s=t[i]:(s=arguments[a],a+=1),n[i]=s,f(s)?o=!0:u-=1,i+=1}return!o&&u<=0?r.apply(this,n):E(Math.max(0,u),tt(e,n,r))}}var et=l(function(t,r){return t===1?g(r):E(t,tt(t,[],r))});function rt(e){return function t(r,n,a){switch(arguments.length){case 0:return t;case 1:return f(r)?t:l(function(u,i){return e(r,u,i)});case 2:return f(r)&&f(n)?t:f(r)?l(function(u,i){return e(u,n,i)}):f(n)?l(function(u,i){return e(r,u,i)}):g(function(u){return e(r,n,u)});default:return f(r)&&f(n)&&f(a)?t:f(r)&&f(n)?l(function(u,i){return e(u,i,a)}):f(r)&&f(a)?l(function(u,i){return e(u,n,i)}):f(n)&&f(a)?l(function(u,i){return e(r,u,i)}):f(r)?g(function(u){return e(u,n,a)}):f(n)?g(function(u){return e(r,u,a)}):f(a)?g(function(u){return e(r,n,u)}):e(r,n,a)}}}var k=Array.isArray||function(t){return t!=null&&t.length>=0&&Object.prototype.toString.call(t)==="[object Array]"};function Lt(e){return e!=null&&typeof e["@@transducer/step"]=="function"}function U(e,t,r){return function(){if(arguments.length===0)return r();var n=arguments[arguments.length-1];if(!k(n)){for(var a=0;a=0;)r=it[n],b(r,t)&&!Ut(a,r)&&(a[a.length]=r),n-=1;return a}),st=g(function(t){return t===null?"Null":t===void 0?"Undefined":Object.prototype.toString.call(t).slice(8,-1)});function ct(e,t,r,n){var a=nt(e),u=nt(t);function i(o,s){return V(o,s,r.slice(),n.slice())}return!at(function(o,s){return!at(i,s,o)},u,a)}function V(e,t,r,n){if(B(e,t))return!0;var a=st(e);if(a!==st(t))return!1;if(typeof e["fantasy-land/equals"]=="function"||typeof t["fantasy-land/equals"]=="function")return typeof e["fantasy-land/equals"]=="function"&&e["fantasy-land/equals"](t)&&typeof t["fantasy-land/equals"]=="function"&&t["fantasy-land/equals"](e);if(typeof e.equals=="function"||typeof t.equals=="function")return typeof e.equals=="function"&&e.equals(t)&&typeof t.equals=="function"&&t.equals(e);switch(a){case"Arguments":case"Array":case"Object":if(typeof e.constructor=="function"&&xt(e.constructor)==="Promise")return e===t;break;case"Boolean":case"Number":case"String":if(!(typeof e==typeof t&&B(e.valueOf(),t.valueOf())))return!1;break;case"Date":if(!B(e.valueOf(),t.valueOf()))return!1;break;case"Error":return e.name===t.name&&e.message===t.message;case"RegExp":if(!(e.source===t.source&&e.global===t.global&&e.ignoreCase===t.ignoreCase&&e.multiline===t.multiline&&e.sticky===t.sticky&&e.unicode===t.unicode))return!1;break}for(var u=r.length-1;u>=0;){if(r[u]===e)return n[u]===t;u-=1}switch(a){case"Map":return e.size!==t.size?!1:ct(e.entries(),t.entries(),r.concat([e]),n.concat([t]));case"Set":return e.size!==t.size?!1:ct(e.values(),t.values(),r.concat([e]),n.concat([t]));case"Arguments":case"Array":case"Object":case"Boolean":case"Number":case"String":case"Date":case"Error":case"RegExp":case"Int8Array":case"Uint8Array":case"Uint8ClampedArray":case"Int16Array":case"Uint16Array":case"Int32Array":case"Uint32Array":case"Float32Array":case"Float64Array":case"ArrayBuffer":break;default:return!1}var i=A(e);if(i.length!==A(t).length)return!1;var o=r.concat([e]),s=n.concat([t]);for(u=i.length-1;u>=0;){var c=i[u];if(!(b(c,t)&&V(t[c],e[c],o,s)))return!1;u-=1}return!0}var N=l(function(t,r){return V(t,r,[],[])});function Bt(e,t,r){var n,a;if(typeof e.indexOf=="function")switch(typeof t){case"number":if(t===0){for(n=1/t;r=0}function z(e,t){for(var r=0,n=t.length,a=Array(n);r":gt(i,o)},n=function(u,i){return z(function(o){return H(o)+": "+r(u[o])},i.slice().sort())};switch(Object.prototype.toString.call(e)){case"[object Arguments]":return"(function() { return arguments; }("+z(r,e).join(", ")+"))";case"[object Array]":return"["+z(r,e).concat(n(e,Zt(function(u){return/^\d+$/.test(u)},A(e)))).join(", ")+"]";case"[object Boolean]":return typeof e=="object"?"new Boolean("+r(e.valueOf())+")":e.toString();case"[object Date]":return"new Date("+(isNaN(e.valueOf())?r(NaN):H(Ht(e)))+")";case"[object Map]":return"new Map("+r(Array.from(e))+")";case"[object Null]":return"null";case"[object Number]":return typeof e=="object"?"new Number("+r(e.valueOf())+")":1/e===-1/0?"-0":e.toString(10);case"[object Set]":return"new Set("+r(Array.from(e).sort())+")";case"[object String]":return typeof e=="object"?"new String("+r(e.valueOf())+")":H(e);case"[object Undefined]":return"undefined";default:if(typeof e.toString=="function"){var a=e.toString();if(a!=="[object Object]")return a}return"{"+n(e,A(e)).join(", ")+"}"}}var pt=g(function(t){return gt(t,[])}),Jt=l(function(t,r){if(t===r)return r;function n(s,c){if(s>c!=c>s)return c>s?c:s}var a=n(t,r);if(a!==void 0)return a;var u=n(typeof t,typeof r);if(u!==void 0)return u===typeof t?t:r;var i=pt(t),o=n(i,pt(r));return o!==void 0&&o===i?t:r}),Qt=function(){function e(t,r){this.xf=r,this.f=t}return e.prototype["@@transducer/init"]=O.init,e.prototype["@@transducer/result"]=O.result,e.prototype["@@transducer/step"]=function(t,r){return this.xf["@@transducer/step"](t,this.f(r))},e}(),Kt=function(t){return function(r){return new Qt(t,r)}},te=l(U(["fantasy-land/map","map"],Kt,function(t,r){switch(Object.prototype.toString.call(r)){case"[object Function]":return et(r.length,function(){return t.call(this,r.apply(this,arguments))});case"[object Object]":return ft(function(n,a){return n[a]=t(r[a]),n},{},A(r));default:return z(t,r)}})),ee=Number.isInteger||function(t){return t<<0===t};function ht(e){return Object.prototype.toString.call(e)==="[object String]"}var yt=l(function(t,r){var n=t<0?r.length+t:t;return ht(r)?r.charAt(n):r[n]}),dt=l(function(t,r){if(r!=null)return ee(t)?yt(t,r):r[t]}),re=g(function(t){return k(t)?!0:!t||typeof t!="object"||ht(t)?!1:t.length===0?!0:t.length>0?t.hasOwnProperty(0)&&t.hasOwnProperty(t.length-1):!1}),mt=typeof Symbol<"u"?Symbol.iterator:"@@iterator";function ne(e,t,r){return function(a,u,i){if(re(i))return e(a,u,i);if(i==null)return u;if(typeof i["fantasy-land/reduce"]=="function")return t(a,u,i,"fantasy-land/reduce");if(i[mt]!=null)return r(a,u,i[mt]());if(typeof i.next=="function")return r(a,u,i);if(typeof i.reduce=="function")return t(a,u,i,"reduce");throw new TypeError("reduce: list must be array or iterable")}}function ae(e,t,r){for(var n=0,a=r.length;n0?(this.n-=1,t):this.xf["@@transducer/step"](t,r)},e}();function de(e){return function(t){return new ye(e,t)}}var me=l(U(["drop"],de,function(t,r){return St(Math.max(0,t),1/0,r)})),ve=yt(-1),we=l(function(t,r){return me(t>=0?r.length-t:0,r)}),Se=l(function e(t,r){if(!lt(r)&&!k(r))return r;var n=r instanceof Array?[]:{},a,u,i;for(u in r)a=t[u],i=typeof a,n[u]=i==="function"?a(r[u]):a&&i==="object"?e(a,r[u]):r[u];return n}),_e=g(function(t){return et(t.length,function(r,n){var a=Array.prototype.slice.call(arguments,0);return a[0]=n,a[1]=r,t.apply(this,a)})});function Oe(e){if(e==null)throw new TypeError("Cannot convert undefined or null to object");for(var t=Object(e),r=1,n=arguments.length;ri?1:0})});const je=400,Ie=10,Te=.8,Ce=e=>e.length>0,Me=(e,t)=>{const r=e.height*t;if(r<=e.width)return{width:r,height:e.height};{const n=e.width/t;return{width:e.width,height:n}}},_t=e=>"url"in e;function*X(e,t){_t(e)?yield t:(yield*X(e.first,t+1),yield*X(e.second,t+1))}const Pe=e=>{const t=[...X(e,0)];if(t.length===0)return 0;const r=Math.max(...t),n=Math.min(...t);return r/(r-n)},Re=({sizeHomogeneity:e,score:t})=>t*t*e,ke=he([[_e(Ee)(9),()=>30],[N(10),()=>25],[N(11),()=>20],[N(12),()=>10],[zt,()=>5]]),Ne=e=>{const t=e[0].pictures.length,r=pe(W(Re),we(ke(t)),W(dt("score")))(e);return Se({pictures:W(dt("url"))},ve(r))},ze=e=>{let t=Number.POSITIVE_INFINITY,r=Number.NEGATIVE_INFINITY;for(let n=0;nr&&(r=u)}return t/r},I=(e,t,r)=>{if(_t(r))return[{position:e,dimension:t,url:r.url}];if(r.horizontal){const n=t.height*r.first.aspectRatio;return[...I(e,{width:n,height:t.height},r.first),...I({x:e.x+n,y:e.y},{width:t.width-n,height:t.height},r.second)]}else{const n=t.width/r.first.aspectRatio;return[...I(e,{width:t.width,height:n},r.first),...I({x:e.x,y:e.y+n},{width:t.width,height:t.height-n},r.second)]}},Le=(e,t,r)=>{const n=I({x:0,y:0},e,r),a=ze(n.map(u=>u.dimension));return{dimension:e,score:t,sizeHomogeneity:a,pictures:n}};class L{constructor(t,r,n){At(this,"aspectRatio",0);this.horizontal=t,this.first=r,this.second=n;const a=r.aspectRatio+n.aspectRatio;this.aspectRatio=t?a:r.aspectRatio*n.aspectRatio/a}}const x=(e,t)=>e+Math.random()*(t-e+1)|0;function*G(e){if(e.length===1)yield e[0];else for(let t=1;t{const t=e.length-1;if(t===0)return e[0];if(t===1)return new L(x(0,1)===0,e[0],e[1]);const r=x(0,t);let n;do n=x(0,t);while(r===n);const a=new L(x(0,1)===0,e[r],e[n]);return Ot(e.with(r,a).toSpliced(n,1))};function*xe(e){for(;;)yield Ot(e)}const qe={maxComputationTime:je,randomizeThreshold:Ie};var De=Object.freeze({__proto__:null,findSolution:(e,t,r)=>{const n=be(r??{},qe),a=Date.now(),u=t.width/t.height,i=[],o=e.length>=n.randomizeThreshold,s=o?xe(e):G(e);for(const c of s){const y=1/(1+Math.abs(c.aspectRatio-u)),h=Pe(c);if(o&&(h<=.7||yn.maxComputationTime)break}if(console.debug("solutions found: ",i.length),!Ce(i))throw new Error("No solution");return Ne(i)}});D(De)})(); + */const m=Symbol("Comlink.proxy"),d=Symbol("Comlink.endpoint"),v=Symbol("Comlink.releaseProxy"),q=Symbol("Comlink.finalizer"),T=Symbol("Comlink.thrown"),Y=e=>typeof e=="object"&&e!==null||typeof e=="function",Et={canHandle:e=>Y(e)&&e[m],serialize(e){const{port1:t,port2:r}=new MessageChannel;return D(e,t),[r,[r]]},deserialize(e){return e.start(),Tt(e)}},bt={canHandle:e=>Y(e)&&T in e,serialize({value:e}){let t;return e instanceof Error?t={isError:!0,value:{message:e.message,name:e.name,stack:e.stack}}:t={isError:!1,value:e},[t,[]]},deserialize(e){throw e.isError?Object.assign(new Error(e.value.message),e.value):e.value}},$=new Map([["proxy",Et],["throw",bt]]);function jt(e,t){for(const r of e)if(t===r||r==="*"||r instanceof RegExp&&r.test(t))return!0;return!1}function D(e,t=globalThis,r=["*"]){t.addEventListener("message",function n(a){if(!a||!a.data)return;if(!jt(r,a.origin)){console.warn(`Invalid origin '${a.origin}' for comlink proxy`);return}const{id:u,type:i,path:o}=Object.assign({path:[]},a.data),s=(a.data.argumentList||[]).map(w);let c;try{const p=o.slice(0,-1).reduce((h,S)=>h[S],e),y=o.reduce((h,S)=>h[S],e);switch(i){case"GET":c=y;break;case"SET":p[o.slice(-1)[0]]=w(a.data.value),c=!0;break;case"APPLY":c=y.apply(p,s);break;case"CONSTRUCT":{const h=new y(...s);c=kt(h)}break;case"ENDPOINT":{const{port1:h,port2:S}=new MessageChannel;D(e,S),c=Rt(h,[h])}break;case"RELEASE":c=void 0;break;default:return}}catch(p){c={value:p,[T]:0}}Promise.resolve(c).catch(p=>({value:p,[T]:0})).then(p=>{const[y,h]=R(p);t.postMessage(Object.assign(Object.assign({},y),{id:u}),h),i==="RELEASE"&&(t.removeEventListener("message",n),Z(t),q in e&&typeof e[q]=="function"&&e[q]())}).catch(p=>{const[y,h]=R({value:new TypeError("Unserializable return value"),[T]:0});t.postMessage(Object.assign(Object.assign({},y),{id:u}),h)})}),t.start&&t.start()}function It(e){return e.constructor.name==="MessagePort"}function Z(e){It(e)&&e.close()}function Tt(e,t){return F(e,[],t)}function C(e){if(e)throw new Error("Proxy has been released and is not useable")}function J(e){return _(e,{type:"RELEASE"}).then(()=>{Z(e)})}const M=new WeakMap,P="FinalizationRegistry"in globalThis&&new FinalizationRegistry(e=>{const t=(M.get(e)||0)-1;M.set(e,t),t===0&&J(e)});function Ct(e,t){const r=(M.get(t)||0)+1;M.set(t,r),P&&P.register(e,t,e)}function Mt(e){P&&P.unregister(e)}function F(e,t=[],r=function(){}){let n=!1;const a=new Proxy(r,{get(u,i){if(C(n),i===v)return()=>{Mt(a),J(e),n=!0};if(i==="then"){if(t.length===0)return{then:()=>a};const o=_(e,{type:"GET",path:t.map(s=>s.toString())}).then(w);return o.then.bind(o)}return F(e,[...t,i])},set(u,i,o){C(n);const[s,c]=R(o);return _(e,{type:"SET",path:[...t,i].map(p=>p.toString()),value:s},c).then(w)},apply(u,i,o){C(n);const s=t[t.length-1];if(s===d)return _(e,{type:"ENDPOINT"}).then(w);if(s==="bind")return F(e,t.slice(0,-1));const[c,p]=Q(o);return _(e,{type:"APPLY",path:t.map(y=>y.toString()),argumentList:c},p).then(w)},construct(u,i){C(n);const[o,s]=Q(i);return _(e,{type:"CONSTRUCT",path:t.map(c=>c.toString()),argumentList:o},s).then(w)}});return Ct(a,e),a}function Pt(e){return Array.prototype.concat.apply([],e)}function Q(e){const t=e.map(R);return[t.map(r=>r[0]),Pt(t.map(r=>r[1]))]}const K=new WeakMap;function Rt(e,t){return K.set(e,t),e}function kt(e){return Object.assign(e,{[m]:!0})}function R(e){for(const[t,r]of $)if(r.canHandle(e)){const[n,a]=r.serialize(e);return[{type:"HANDLER",name:t,value:n},a]}return[{type:"RAW",value:e},K.get(e)||[]]}function w(e){switch(e.type){case"HANDLER":return $.get(e.name).deserialize(e.value);case"RAW":return e.value}}function _(e,t,r){return new Promise(n=>{const a=Nt();e.addEventListener("message",function u(i){!i.data||!i.data.id||i.data.id!==a||(e.removeEventListener("message",u),n(i.data))}),e.start&&e.start(),e.postMessage(Object.assign({id:a},t),r)})}function Nt(){return new Array(4).fill(0).map(()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16)).join("-")}var zt=function(){return!0};function f(e){return e!=null&&typeof e=="object"&&e["@@functional/placeholder"]===!0}function g(e){return function t(r){return arguments.length===0||f(r)?t:e.apply(this,arguments)}}function l(e){return function t(r,n){switch(arguments.length){case 0:return t;case 1:return f(r)?t:g(function(a){return e(r,a)});default:return f(r)&&f(n)?t:f(r)?g(function(a){return e(a,n)}):f(n)?g(function(a){return e(r,a)}):e(r,n)}}}function E(e,t){switch(e){case 0:return function(){return t.apply(this,arguments)};case 1:return function(r){return t.apply(this,arguments)};case 2:return function(r,n){return t.apply(this,arguments)};case 3:return function(r,n,a){return t.apply(this,arguments)};case 4:return function(r,n,a,u){return t.apply(this,arguments)};case 5:return function(r,n,a,u,i){return t.apply(this,arguments)};case 6:return function(r,n,a,u,i,o){return t.apply(this,arguments)};case 7:return function(r,n,a,u,i,o,s){return t.apply(this,arguments)};case 8:return function(r,n,a,u,i,o,s,c){return t.apply(this,arguments)};case 9:return function(r,n,a,u,i,o,s,c,p){return t.apply(this,arguments)};case 10:return function(r,n,a,u,i,o,s,c,p,y){return t.apply(this,arguments)};default:throw new Error("First argument to _arity must be a non-negative integer no greater than ten")}}function tt(e,t,r){return function(){for(var n=[],a=0,u=e,i=0,o=!1;i=arguments.length)?s=t[i]:(s=arguments[a],a+=1),n[i]=s,f(s)?o=!0:u-=1,i+=1}return!o&&u<=0?r.apply(this,n):E(Math.max(0,u),tt(e,n,r))}}var et=l(function(t,r){return t===1?g(r):E(t,tt(t,[],r))});function rt(e){return function t(r,n,a){switch(arguments.length){case 0:return t;case 1:return f(r)?t:l(function(u,i){return e(r,u,i)});case 2:return f(r)&&f(n)?t:f(r)?l(function(u,i){return e(u,n,i)}):f(n)?l(function(u,i){return e(r,u,i)}):g(function(u){return e(r,n,u)});default:return f(r)&&f(n)&&f(a)?t:f(r)&&f(n)?l(function(u,i){return e(u,i,a)}):f(r)&&f(a)?l(function(u,i){return e(u,n,i)}):f(n)&&f(a)?l(function(u,i){return e(r,u,i)}):f(r)?g(function(u){return e(u,n,a)}):f(n)?g(function(u){return e(r,u,a)}):f(a)?g(function(u){return e(r,n,u)}):e(r,n,a)}}}var k=Array.isArray||function(t){return t!=null&&t.length>=0&&Object.prototype.toString.call(t)==="[object Array]"};function Lt(e){return e!=null&&typeof e["@@transducer/step"]=="function"}function U(e,t,r){return function(){if(arguments.length===0)return r();var n=arguments[arguments.length-1];if(!k(n)){for(var a=0;a=0;)r=it[n],b(r,t)&&!Ut(a,r)&&(a[a.length]=r),n-=1;return a}),st=g(function(t){return t===null?"Null":t===void 0?"Undefined":Object.prototype.toString.call(t).slice(8,-1)});function ct(e,t,r,n){var a=nt(e),u=nt(t);function i(o,s){return V(o,s,r.slice(),n.slice())}return!at(function(o,s){return!at(i,s,o)},u,a)}function V(e,t,r,n){if(B(e,t))return!0;var a=st(e);if(a!==st(t))return!1;if(typeof e["fantasy-land/equals"]=="function"||typeof t["fantasy-land/equals"]=="function")return typeof e["fantasy-land/equals"]=="function"&&e["fantasy-land/equals"](t)&&typeof t["fantasy-land/equals"]=="function"&&t["fantasy-land/equals"](e);if(typeof e.equals=="function"||typeof t.equals=="function")return typeof e.equals=="function"&&e.equals(t)&&typeof t.equals=="function"&&t.equals(e);switch(a){case"Arguments":case"Array":case"Object":if(typeof e.constructor=="function"&&xt(e.constructor)==="Promise")return e===t;break;case"Boolean":case"Number":case"String":if(!(typeof e==typeof t&&B(e.valueOf(),t.valueOf())))return!1;break;case"Date":if(!B(e.valueOf(),t.valueOf()))return!1;break;case"Error":return e.name===t.name&&e.message===t.message;case"RegExp":if(!(e.source===t.source&&e.global===t.global&&e.ignoreCase===t.ignoreCase&&e.multiline===t.multiline&&e.sticky===t.sticky&&e.unicode===t.unicode))return!1;break}for(var u=r.length-1;u>=0;){if(r[u]===e)return n[u]===t;u-=1}switch(a){case"Map":return e.size!==t.size?!1:ct(e.entries(),t.entries(),r.concat([e]),n.concat([t]));case"Set":return e.size!==t.size?!1:ct(e.values(),t.values(),r.concat([e]),n.concat([t]));case"Arguments":case"Array":case"Object":case"Boolean":case"Number":case"String":case"Date":case"Error":case"RegExp":case"Int8Array":case"Uint8Array":case"Uint8ClampedArray":case"Int16Array":case"Uint16Array":case"Int32Array":case"Uint32Array":case"Float32Array":case"Float64Array":case"ArrayBuffer":break;default:return!1}var i=A(e);if(i.length!==A(t).length)return!1;var o=r.concat([e]),s=n.concat([t]);for(u=i.length-1;u>=0;){var c=i[u];if(!(b(c,t)&&V(t[c],e[c],o,s)))return!1;u-=1}return!0}var N=l(function(t,r){return V(t,r,[],[])});function Bt(e,t,r){var n,a;if(typeof e.indexOf=="function")switch(typeof t){case"number":if(t===0){for(n=1/t;r=0}function z(e,t){for(var r=0,n=t.length,a=Array(n);r":gt(i,o)},n=function(u,i){return z(function(o){return H(o)+": "+r(u[o])},i.slice().sort())};switch(Object.prototype.toString.call(e)){case"[object Arguments]":return"(function() { return arguments; }("+z(r,e).join(", ")+"))";case"[object Array]":return"["+z(r,e).concat(n(e,Zt(function(u){return/^\d+$/.test(u)},A(e)))).join(", ")+"]";case"[object Boolean]":return typeof e=="object"?"new Boolean("+r(e.valueOf())+")":e.toString();case"[object Date]":return"new Date("+(isNaN(e.valueOf())?r(NaN):H(Ht(e)))+")";case"[object Map]":return"new Map("+r(Array.from(e))+")";case"[object Null]":return"null";case"[object Number]":return typeof e=="object"?"new Number("+r(e.valueOf())+")":1/e===-1/0?"-0":e.toString(10);case"[object Set]":return"new Set("+r(Array.from(e).sort())+")";case"[object String]":return typeof e=="object"?"new String("+r(e.valueOf())+")":H(e);case"[object Undefined]":return"undefined";default:if(typeof e.toString=="function"){var a=e.toString();if(a!=="[object Object]")return a}return"{"+n(e,A(e)).join(", ")+"}"}}var pt=g(function(t){return gt(t,[])}),Jt=l(function(t,r){if(t===r)return r;function n(s,c){if(s>c!=c>s)return c>s?c:s}var a=n(t,r);if(a!==void 0)return a;var u=n(typeof t,typeof r);if(u!==void 0)return u===typeof t?t:r;var i=pt(t),o=n(i,pt(r));return o!==void 0&&o===i?t:r}),Qt=function(){function e(t,r){this.xf=r,this.f=t}return e.prototype["@@transducer/init"]=O.init,e.prototype["@@transducer/result"]=O.result,e.prototype["@@transducer/step"]=function(t,r){return this.xf["@@transducer/step"](t,this.f(r))},e}(),Kt=function(t){return function(r){return new Qt(t,r)}},te=l(U(["fantasy-land/map","map"],Kt,function(t,r){switch(Object.prototype.toString.call(r)){case"[object Function]":return et(r.length,function(){return t.call(this,r.apply(this,arguments))});case"[object Object]":return ft(function(n,a){return n[a]=t(r[a]),n},{},A(r));default:return z(t,r)}})),ee=Number.isInteger||function(t){return t<<0===t};function ht(e){return Object.prototype.toString.call(e)==="[object String]"}var yt=l(function(t,r){var n=t<0?r.length+t:t;return ht(r)?r.charAt(n):r[n]}),dt=l(function(t,r){if(r!=null)return ee(t)?yt(t,r):r[t]}),re=g(function(t){return k(t)?!0:!t||typeof t!="object"||ht(t)?!1:t.length===0?!0:t.length>0?t.hasOwnProperty(0)&&t.hasOwnProperty(t.length-1):!1}),mt=typeof Symbol<"u"?Symbol.iterator:"@@iterator";function ne(e,t,r){return function(a,u,i){if(re(i))return e(a,u,i);if(i==null)return u;if(typeof i["fantasy-land/reduce"]=="function")return t(a,u,i,"fantasy-land/reduce");if(i[mt]!=null)return r(a,u,i[mt]());if(typeof i.next=="function")return r(a,u,i);if(typeof i.reduce=="function")return t(a,u,i,"reduce");throw new TypeError("reduce: list must be array or iterable")}}function ae(e,t,r){for(var n=0,a=r.length;n0?(this.n-=1,t):this.xf["@@transducer/step"](t,r)},e}();function de(e){return function(t){return new ye(e,t)}}var me=l(U(["drop"],de,function(t,r){return St(Math.max(0,t),1/0,r)})),ve=yt(-1),we=l(function(t,r){return me(t>=0?r.length-t:0,r)}),Se=l(function e(t,r){if(!lt(r)&&!k(r))return r;var n=r instanceof Array?[]:{},a,u,i;for(u in r)a=t[u],i=typeof a,n[u]=i==="function"?a(r[u]):a&&i==="object"?e(a,r[u]):r[u];return n}),_e=g(function(t){return et(t.length,function(r,n){var a=Array.prototype.slice.call(arguments,0);return a[0]=n,a[1]=r,t.apply(this,a)})});function Oe(e){if(e==null)throw new TypeError("Cannot convert undefined or null to object");for(var t=Object(e),r=1,n=arguments.length;ri?1:0})});const je=400,Ie=10,Te=.8,Ce=e=>e.length>0,Me=(e,t)=>{const r=e.height*t;if(r<=e.width)return{width:r,height:e.height};{const n=e.width/t;return{width:e.width,height:n}}},_t=e=>"url"in e;function*X(e,t){_t(e)?yield t:(yield*X(e.first,t+1),yield*X(e.second,t+1))}const Pe=e=>{const t=[...X(e,0)];if(t.length===0)return 0;const r=Math.max(...t),n=Math.min(...t);return 1-(r-n)/r},Re=({sizeHomogeneity:e,score:t})=>t*t*e,ke=he([[_e(Ee)(9),()=>30],[N(10),()=>25],[N(11),()=>20],[N(12),()=>10],[zt,()=>5]]),Ne=e=>{const t=e[0].pictures.length,r=pe(W(Re),we(ke(t)),W(dt("score")))(e);return Se({pictures:W(dt("url"))},ve(r))},ze=e=>{let t=Number.POSITIVE_INFINITY,r=Number.NEGATIVE_INFINITY;for(let n=0;nr&&(r=u)}return t/r},I=(e,t,r)=>{if(_t(r))return[{position:e,dimension:t,url:r.url}];if(r.horizontal){const n=t.height*r.first.aspectRatio;return[...I(e,{width:n,height:t.height},r.first),...I({x:e.x+n,y:e.y},{width:t.width-n,height:t.height},r.second)]}else{const n=t.width/r.first.aspectRatio;return[...I(e,{width:t.width,height:n},r.first),...I({x:e.x,y:e.y+n},{width:t.width,height:t.height-n},r.second)]}},Le=(e,t,r)=>{const n=I({x:0,y:0},e,r),a=ze(n.map(u=>u.dimension));return{dimension:e,score:t,sizeHomogeneity:a,pictures:n}};class L{constructor(t,r,n){At(this,"aspectRatio",0);this.horizontal=t,this.first=r,this.second=n;const a=r.aspectRatio+n.aspectRatio;this.aspectRatio=t?a:r.aspectRatio*n.aspectRatio/a}}const x=(e,t)=>e+Math.random()*(t-e+1)|0;function*G(e){if(e.length===1)yield e[0];else for(let t=1;t{const t=e.length-1;if(t===0)return e[0];if(t===1)return new L(x(0,1)===0,e[0],e[1]);const r=x(0,t);let n;do n=x(0,t);while(r===n);const a=new L(x(0,1)===0,e[r],e[n]);return Ot(e.with(r,a).toSpliced(n,1))};function*xe(e){for(;;)yield Ot(e)}const qe={maxComputationTime:je,randomizeThreshold:Ie};var De=Object.freeze({__proto__:null,findSolution:(e,t,r)=>{const n=be(r??{},qe),a=Date.now(),u=t.width/t.height,i=[],o=e.length>=n.randomizeThreshold,s=o?xe(e):G(e);for(const c of s){const y=1/(1+Math.abs(c.aspectRatio-u)),h=Pe(c);if(o&&(i.length>0&&h<=.6||yn.maxComputationTime)break}if(console.debug("solutions found: ",i.length),!Ce(i))throw new Error("No solution");return Ne(i)}});D(De)})(); diff --git a/docs/none/index-DGsLmoZt.js b/docs/none/index-HpxU7qA1.js similarity index 99% rename from docs/none/index-DGsLmoZt.js rename to docs/none/index-HpxU7qA1.js index ced92db..1b13599 100644 --- a/docs/none/index-DGsLmoZt.js +++ b/docs/none/index-HpxU7qA1.js @@ -41,7 +41,7 @@ Error generating stack: `+o.message+` * @license * Copyright 2019 Google LLC * SPDX-License-Identifier: Apache-2.0 -*/const hp=Symbol("Comlink.proxy"),Fy=Symbol("Comlink.endpoint"),Iy=Symbol("Comlink.releaseProxy"),Oi=Symbol("Comlink.finalizer"),Ql=Symbol("Comlink.thrown"),mp=e=>typeof e=="object"&&e!==null||typeof e=="function",Dy={canHandle:e=>mp(e)&&e[hp],serialize(e){const{port1:t,port2:n}=new MessageChannel;return yp(e,t),[n,[n]]},deserialize(e){return e.start(),wp(e)}},Ay={canHandle:e=>mp(e)&&Ql in e,serialize({value:e}){let t;return e instanceof Error?t={isError:!0,value:{message:e.message,name:e.name,stack:e.stack}}:t={isError:!1,value:e},[t,[]]},deserialize(e){throw e.isError?Object.assign(new Error(e.value.message),e.value):e.value}},vp=new Map([["proxy",Dy],["throw",Ay]]);function Uy(e,t){for(const n of e)if(t===n||n==="*"||n instanceof RegExp&&n.test(t))return!0;return!1}function yp(e,t=globalThis,n=["*"]){t.addEventListener("message",function r(l){if(!l||!l.data)return;if(!Uy(n,l.origin)){console.warn(`Invalid origin '${l.origin}' for comlink proxy`);return}const{id:o,type:i,path:u}=Object.assign({path:[]},l.data),s=(l.data.argumentList||[]).map(Jt);let a;try{const h=u.slice(0,-1).reduce((p,g)=>p[g],e),m=u.reduce((p,g)=>p[g],e);switch(i){case"GET":a=m;break;case"SET":h[u.slice(-1)[0]]=Jt(l.data.value),a=!0;break;case"APPLY":a=m.apply(h,s);break;case"CONSTRUCT":{const p=new m(...s);a=Ky(p)}break;case"ENDPOINT":{const{port1:p,port2:g}=new MessageChannel;yp(e,g),a=Qy(p,[p])}break;case"RELEASE":a=void 0;break;default:return}}catch(h){a={value:h,[Ql]:0}}Promise.resolve(a).catch(h=>({value:h,[Ql]:0})).then(h=>{const[m,p]=_o(h);t.postMessage(Object.assign(Object.assign({},m),{id:o}),p),i==="RELEASE"&&(t.removeEventListener("message",r),gp(t),Oi in e&&typeof e[Oi]=="function"&&e[Oi]())}).catch(h=>{const[m,p]=_o({value:new TypeError("Unserializable return value"),[Ql]:0});t.postMessage(Object.assign(Object.assign({},m),{id:o}),p)})}),t.start&&t.start()}function Vy(e){return e.constructor.name==="MessagePort"}function gp(e){Vy(e)&&e.close()}function wp(e,t){return Tu(e,[],t)}function zl(e){if(e)throw new Error("Proxy has been released and is not useable")}function Sp(e){return mn(e,{type:"RELEASE"}).then(()=>{gp(e)})}const So=new WeakMap,ko="FinalizationRegistry"in globalThis&&new FinalizationRegistry(e=>{const t=(So.get(e)||0)-1;So.set(e,t),t===0&&Sp(e)});function By(e,t){const n=(So.get(t)||0)+1;So.set(t,n),ko&&ko.register(e,t,e)}function Wy(e){ko&&ko.unregister(e)}function Tu(e,t=[],n=function(){}){let r=!1;const l=new Proxy(n,{get(o,i){if(zl(r),i===Iy)return()=>{Wy(l),Sp(e),r=!0};if(i==="then"){if(t.length===0)return{then:()=>l};const u=mn(e,{type:"GET",path:t.map(s=>s.toString())}).then(Jt);return u.then.bind(u)}return Tu(e,[...t,i])},set(o,i,u){zl(r);const[s,a]=_o(u);return mn(e,{type:"SET",path:[...t,i].map(h=>h.toString()),value:s},a).then(Jt)},apply(o,i,u){zl(r);const s=t[t.length-1];if(s===Fy)return mn(e,{type:"ENDPOINT"}).then(Jt);if(s==="bind")return Tu(e,t.slice(0,-1));const[a,h]=Cc(u);return mn(e,{type:"APPLY",path:t.map(m=>m.toString()),argumentList:a},h).then(Jt)},construct(o,i){zl(r);const[u,s]=Cc(i);return mn(e,{type:"CONSTRUCT",path:t.map(a=>a.toString()),argumentList:u},s).then(Jt)}});return By(l,e),l}function Hy(e){return Array.prototype.concat.apply([],e)}function Cc(e){const t=e.map(_o);return[t.map(n=>n[0]),Hy(t.map(n=>n[1]))]}const kp=new WeakMap;function Qy(e,t){return kp.set(e,t),e}function Ky(e){return Object.assign(e,{[hp]:!0})}function _o(e){for(const[t,n]of vp)if(n.canHandle(e)){const[r,l]=n.serialize(e);return[{type:"HANDLER",name:t,value:r},l]}return[{type:"RAW",value:e},kp.get(e)||[]]}function Jt(e){switch(e.type){case"HANDLER":return vp.get(e.name).deserialize(e.value);case"RAW":return e.value}}function mn(e,t,n){return new Promise(r=>{const l=qy();e.addEventListener("message",function o(i){!i.data||!i.data.id||i.data.id!==l||(e.removeEventListener("message",o),r(i.data))}),e.start&&e.start(),e.postMessage(Object.assign({id:l},t),n)})}function qy(){return new Array(4).fill(0).map(()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16)).join("-")}const Yy=wp(new Worker(new URL("/diorama-2023/none/find-solution-Cjopq3Ht.js",import.meta.url))),Xy=(e,t,n)=>Yy.findSolution(e,t,n),Gy=e=>e.length>0,Jy=e=>new Promise((t,n)=>{const r=new Image;r.onload=()=>t({dimension:{width:r.width,height:r.height},aspectRatio:r.width/r.height,url:e}),r.onerror=n,r.src=e}),Zy=e=>{const t=Ry(1,av[e]).map(n=>`./images/${Fn[e]}/${n}.jpg`);return Promise.all(t.map(Jy))},Hs=lt([]),by=Hs.map($y),ol=lt(Hd),_p=lt([]),Ep=lt(null),Qs=lt({width:0,height:0}),Ks=lt(!1),er=Kd(e=>Promise.all([Zy(e),new Promise(t=>setTimeout(t,500))]).then(qo(0))),xp=De(),Cp=De(),Np=De(),Pp=De(),eg=Fs(ol,by);Uo({source:eg,clock:xp,fn:([e,t])=>Ty(t,e+1),target:ol});ol.on(Cp,Bs(e=>hy(1,e-1)));Hs.on(er.doneData,Ws(1));ol.on(er.doneData,()=>Hd);Ks.on(er,Yv);Ks.on(er.done,qv);Qs.on(Pp,Ws(1));Uo({source:[Hs,ol],fn:([e,t])=>e.slice(0,t),target:_p});const zp=Fs(_p,Qs),Lp=([e,t])=>Gy(e)&&t.width>0&&t.height>0,qs=Kd(([e,t])=>Xy(e,t));Ep.on(qs.doneData,Ws(1));Uo({source:zp,clock:Np,filter:Lp,target:qs});Uo({source:zp,filter:Lp,target:qs});const tg=jy(["target","value"]),ng=()=>A.jsxs("ol",{className:"controls",children:[A.jsx("li",{children:A.jsxs("select",{onChange:Bs(tg,er),children:[A.jsx("option",{value:Fn.animals,children:"Animals"}),A.jsx("option",{value:Fn.family,children:"Photos"}),A.jsx("option",{value:Fn.art,children:"Art"})]})}),A.jsx("li",{children:A.jsx("button",{onClick:()=>Np(),title:"rearrange",children:A.jsx(vv,{size:Cl,color:"white",strokeWidth:1.5})})}),A.jsx("li",{children:A.jsx("button",{onClick:()=>xp(),title:"add image",children:A.jsx(hv,{size:Cl,color:"white",strokeWidth:1.5})})}),A.jsx("li",{children:A.jsx("button",{onClick:()=>Cp(),title:"remove image",children:A.jsx(dv,{size:Cl,color:"white",strokeWidth:1.5})})}),ic.isEnabled&&A.jsx("li",{children:A.jsx("button",{onClick:()=>void ic.request(),title:"fullscreen",children:A.jsx(gv,{size:Cl,color:"white",strokeWidth:1.5})})})]}),rg=({picture:{position:e,dimension:t,url:n},scaleX:r,scaleY:l,idx:o})=>{const i=t.width/t.height;return A.jsx("li",{className:"photo-frame",style:{left:e.x*r,top:e.y*l,width:t.width*r,height:t.height*l},children:A.jsx("img",{className:"photo",src:n,alt:"",style:{aspectRatio:i}})})};var $p={exports:{}},Op={},jp={exports:{}},Tp={};/** +*/const hp=Symbol("Comlink.proxy"),Fy=Symbol("Comlink.endpoint"),Iy=Symbol("Comlink.releaseProxy"),Oi=Symbol("Comlink.finalizer"),Ql=Symbol("Comlink.thrown"),mp=e=>typeof e=="object"&&e!==null||typeof e=="function",Dy={canHandle:e=>mp(e)&&e[hp],serialize(e){const{port1:t,port2:n}=new MessageChannel;return yp(e,t),[n,[n]]},deserialize(e){return e.start(),wp(e)}},Ay={canHandle:e=>mp(e)&&Ql in e,serialize({value:e}){let t;return e instanceof Error?t={isError:!0,value:{message:e.message,name:e.name,stack:e.stack}}:t={isError:!1,value:e},[t,[]]},deserialize(e){throw e.isError?Object.assign(new Error(e.value.message),e.value):e.value}},vp=new Map([["proxy",Dy],["throw",Ay]]);function Uy(e,t){for(const n of e)if(t===n||n==="*"||n instanceof RegExp&&n.test(t))return!0;return!1}function yp(e,t=globalThis,n=["*"]){t.addEventListener("message",function r(l){if(!l||!l.data)return;if(!Uy(n,l.origin)){console.warn(`Invalid origin '${l.origin}' for comlink proxy`);return}const{id:o,type:i,path:u}=Object.assign({path:[]},l.data),s=(l.data.argumentList||[]).map(Jt);let a;try{const h=u.slice(0,-1).reduce((p,g)=>p[g],e),m=u.reduce((p,g)=>p[g],e);switch(i){case"GET":a=m;break;case"SET":h[u.slice(-1)[0]]=Jt(l.data.value),a=!0;break;case"APPLY":a=m.apply(h,s);break;case"CONSTRUCT":{const p=new m(...s);a=Ky(p)}break;case"ENDPOINT":{const{port1:p,port2:g}=new MessageChannel;yp(e,g),a=Qy(p,[p])}break;case"RELEASE":a=void 0;break;default:return}}catch(h){a={value:h,[Ql]:0}}Promise.resolve(a).catch(h=>({value:h,[Ql]:0})).then(h=>{const[m,p]=_o(h);t.postMessage(Object.assign(Object.assign({},m),{id:o}),p),i==="RELEASE"&&(t.removeEventListener("message",r),gp(t),Oi in e&&typeof e[Oi]=="function"&&e[Oi]())}).catch(h=>{const[m,p]=_o({value:new TypeError("Unserializable return value"),[Ql]:0});t.postMessage(Object.assign(Object.assign({},m),{id:o}),p)})}),t.start&&t.start()}function Vy(e){return e.constructor.name==="MessagePort"}function gp(e){Vy(e)&&e.close()}function wp(e,t){return Tu(e,[],t)}function zl(e){if(e)throw new Error("Proxy has been released and is not useable")}function Sp(e){return mn(e,{type:"RELEASE"}).then(()=>{gp(e)})}const So=new WeakMap,ko="FinalizationRegistry"in globalThis&&new FinalizationRegistry(e=>{const t=(So.get(e)||0)-1;So.set(e,t),t===0&&Sp(e)});function By(e,t){const n=(So.get(t)||0)+1;So.set(t,n),ko&&ko.register(e,t,e)}function Wy(e){ko&&ko.unregister(e)}function Tu(e,t=[],n=function(){}){let r=!1;const l=new Proxy(n,{get(o,i){if(zl(r),i===Iy)return()=>{Wy(l),Sp(e),r=!0};if(i==="then"){if(t.length===0)return{then:()=>l};const u=mn(e,{type:"GET",path:t.map(s=>s.toString())}).then(Jt);return u.then.bind(u)}return Tu(e,[...t,i])},set(o,i,u){zl(r);const[s,a]=_o(u);return mn(e,{type:"SET",path:[...t,i].map(h=>h.toString()),value:s},a).then(Jt)},apply(o,i,u){zl(r);const s=t[t.length-1];if(s===Fy)return mn(e,{type:"ENDPOINT"}).then(Jt);if(s==="bind")return Tu(e,t.slice(0,-1));const[a,h]=Cc(u);return mn(e,{type:"APPLY",path:t.map(m=>m.toString()),argumentList:a},h).then(Jt)},construct(o,i){zl(r);const[u,s]=Cc(i);return mn(e,{type:"CONSTRUCT",path:t.map(a=>a.toString()),argumentList:u},s).then(Jt)}});return By(l,e),l}function Hy(e){return Array.prototype.concat.apply([],e)}function Cc(e){const t=e.map(_o);return[t.map(n=>n[0]),Hy(t.map(n=>n[1]))]}const kp=new WeakMap;function Qy(e,t){return kp.set(e,t),e}function Ky(e){return Object.assign(e,{[hp]:!0})}function _o(e){for(const[t,n]of vp)if(n.canHandle(e)){const[r,l]=n.serialize(e);return[{type:"HANDLER",name:t,value:r},l]}return[{type:"RAW",value:e},kp.get(e)||[]]}function Jt(e){switch(e.type){case"HANDLER":return vp.get(e.name).deserialize(e.value);case"RAW":return e.value}}function mn(e,t,n){return new Promise(r=>{const l=qy();e.addEventListener("message",function o(i){!i.data||!i.data.id||i.data.id!==l||(e.removeEventListener("message",o),r(i.data))}),e.start&&e.start(),e.postMessage(Object.assign({id:l},t),n)})}function qy(){return new Array(4).fill(0).map(()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16)).join("-")}const Yy=wp(new Worker(new URL("/diorama-2023/none/find-solution-C0sgTXPj.js",import.meta.url))),Xy=(e,t,n)=>Yy.findSolution(e,t,n),Gy=e=>e.length>0,Jy=e=>new Promise((t,n)=>{const r=new Image;r.onload=()=>t({dimension:{width:r.width,height:r.height},aspectRatio:r.width/r.height,url:e}),r.onerror=n,r.src=e}),Zy=e=>{const t=Ry(1,av[e]).map(n=>`./images/${Fn[e]}/${n}.jpg`);return Promise.all(t.map(Jy))},Hs=lt([]),by=Hs.map($y),ol=lt(Hd),_p=lt([]),Ep=lt(null),Qs=lt({width:0,height:0}),Ks=lt(!1),er=Kd(e=>Promise.all([Zy(e),new Promise(t=>setTimeout(t,500))]).then(qo(0))),xp=De(),Cp=De(),Np=De(),Pp=De(),eg=Fs(ol,by);Uo({source:eg,clock:xp,fn:([e,t])=>Ty(t,e+1),target:ol});ol.on(Cp,Bs(e=>hy(1,e-1)));Hs.on(er.doneData,Ws(1));ol.on(er.doneData,()=>Hd);Ks.on(er,Yv);Ks.on(er.done,qv);Qs.on(Pp,Ws(1));Uo({source:[Hs,ol],fn:([e,t])=>e.slice(0,t),target:_p});const zp=Fs(_p,Qs),Lp=([e,t])=>Gy(e)&&t.width>0&&t.height>0,qs=Kd(([e,t])=>Xy(e,t));Ep.on(qs.doneData,Ws(1));Uo({source:zp,clock:Np,filter:Lp,target:qs});Uo({source:zp,filter:Lp,target:qs});const tg=jy(["target","value"]),ng=()=>A.jsxs("ol",{className:"controls",children:[A.jsx("li",{children:A.jsxs("select",{onChange:Bs(tg,er),children:[A.jsx("option",{value:Fn.animals,children:"Animals"}),A.jsx("option",{value:Fn.family,children:"Photos"}),A.jsx("option",{value:Fn.art,children:"Art"})]})}),A.jsx("li",{children:A.jsx("button",{onClick:()=>Np(),title:"rearrange",children:A.jsx(vv,{size:Cl,color:"white",strokeWidth:1.5})})}),A.jsx("li",{children:A.jsx("button",{onClick:()=>xp(),title:"add image",children:A.jsx(hv,{size:Cl,color:"white",strokeWidth:1.5})})}),A.jsx("li",{children:A.jsx("button",{onClick:()=>Cp(),title:"remove image",children:A.jsx(dv,{size:Cl,color:"white",strokeWidth:1.5})})}),ic.isEnabled&&A.jsx("li",{children:A.jsx("button",{onClick:()=>void ic.request(),title:"fullscreen",children:A.jsx(gv,{size:Cl,color:"white",strokeWidth:1.5})})})]}),rg=({picture:{position:e,dimension:t,url:n},scaleX:r,scaleY:l,idx:o})=>{const i=t.width/t.height;return A.jsx("li",{className:"photo-frame",style:{left:e.x*r,top:e.y*l,width:t.width*r,height:t.height*l},children:A.jsx("img",{className:"photo",src:n,alt:"",style:{aspectRatio:i}})})};var $p={exports:{}},Op={},jp={exports:{}},Tp={};/** * @license React * use-sync-external-store-shim.production.min.js * diff --git a/src/layout/find-solution.ts b/src/layout/find-solution.ts index 3699d4e..e1cc6cb 100644 --- a/src/layout/find-solution.ts +++ b/src/layout/find-solution.ts @@ -37,7 +37,11 @@ export const findSolution = ( const distance = Math.abs(root.aspectRatio - arTarget) const score = 1 / (1 + distance) const balance = treeBalance(root) - if (runForever && (balance <= 0.7 || score < discardBadRatio)) { + + if ( + runForever && + ((solutions.length > 0 && balance <= 0.6) || score < discardBadRatio) + ) { continue } const actualDimensions = resizeDimension(targetDimension, root.aspectRatio) diff --git a/src/utils/tree-balance.ts b/src/utils/tree-balance.ts index 232e5bf..89e1381 100644 --- a/src/utils/tree-balance.ts +++ b/src/utils/tree-balance.ts @@ -23,5 +23,6 @@ export const treeBalance = (node: Rect): number => { // Calculate normalized balance const minLeafDepth = Math.min(...leafDepths) - return maxDepth / (maxDepth - minLeafDepth) + + return 1 - (maxDepth - minLeafDepth) / maxDepth }