1
0
mirror of https://github.com/Chouchen/motsmeles.git synced 2018-06-07 06:04:38 +02:00
motsmeles/dist/js/motsmeles.min.js

2 lines
3.3 KiB
JavaScript
Raw Normal View History

2015-01-29 00:21:03 +01:00
/*! motsmeles 2015-01-29 */
2015-02-15 22:58:56 +01:00
var SIZE=30,GRID=[["S","S","S","A","U","B","A","G","N","E"],["E","P","A","A","M","V","E","N","C","E"],["L","U","L","M","I","R","A","M","A","S"],["R","A","O","G","E","X","A","C","O","E"],["A","L","N","E","G","N","A","R","O","R"],["T","O","U","L","O","N","G","L","R","E"],["N","D","S","T","N","U","E","I","E","Y"],["I","N","P","E","E","N","I","L","D","H"],["C","A","S","S","U","J","E","R","F","L"],["E","B","E","L","S","I","S","S","A","C"]],words=["AIX","APT","ARLES","AUBAGNE","AUPS","AVIGNON","BANDOL","CANNES","CASSIS","DIGNE","FREJUS","HYERES","LUNEL","MIRAMAS","NICE","ORANGE","SALON","SORGUES","TOULON","VENCE"];!function(a,b,c,d){var e,f,g,h,i=new a.Canvas("c",{selection:!1}),j=[],k=[],l=!1,m=a.util.createClass(a.Rect,{type:"labeledRect",initialize:function(a){a=a||{},this.callSuper("initialize",a),this.set("label",a.label||"")},toObject:function(){return a.util.object.extend(this.callSuper("toObject"),{label:this.get("label")})},_render:function(a){this.callSuper("_render",a),a.font="20px Helvetica",a.fillStyle="#333",a.fillText(this.label,-this.width/2+5,-this.height/2+20)}}),n=function(){var a,b,e,f=-1,g=j[0]===k[0],h=j[1]===k[1],i=Math.abs(j[0]-k[0])===Math.abs(j[1]-k[1]),l="";if(g){for(b=Math.min(j[1],k[1]),e=Math.max(j[1],k[1]),a=b;e>=a;a++)l+=c[j[0]][a];return Math.max(d.indexOf(l),d.indexOf(l.split("").reverse().join("")))}if(h){for(b=Math.min(j[0],k[0]),e=Math.max(j[0],k[0]),a=b;e>=a;a++)l+=c[a][j[1]];return Math.max(d.indexOf(l),d.indexOf(l.split("").reverse().join("")))}if(i){for(var m=j[0]-k[0]<0,n=j[1]-k[1]<0,o=0,p=Math.abs(j[0]-k[0]);p>=o;o++)l+=c[m?j[0]+o:j[0]-o][n?j[1]+o:j[1]-o];return Math.max(d.indexOf(l),d.indexOf(l.split("").reverse().join("")))}return f},o=function(a){var b=document.getElementById("words").getElementsByTagName("li"),c=b.length;for(g=0;c>g;g++)if(b[g].textContent===a){b[g].className="validate";break}},p=function(){var a,b=1,d=c.length;if(0!==d&&c[0]){for(a=c[0].length;d>b;b++)if(c[b].length!==a)return!1;return!0}return!1},q=function(){if(!p())throw new Error("invalid grid");var a,g=c.length,h=c[0].length,j=document.createDocumentFragment();for(i.setDimensions({width:b*h+h,height:b*g+g}),e=0;h>e;e++)for(f=0;g>f;f++){var k=new m({left:e*b,top:f*b,fill:"white",stroke:"black",width:b,height:b,label:c[f][e],selectable:!1});i.add(k)}d.forEach(function(b){a=document.createElement("li"),a.innerHTML=b.toUpperCase(),j.appendChild(a)}),document.getElementById("words").appendChild(j)};i.on("mouse:down",function(c){l=!0;var d=i.getPointer(c.e),e=[d.x,d.y,d.x,d.y];j=[Math.floor(e[1]/b),Math.floor(e[0]/b)],h=new a.Line(e,{stroke:"black",strokeLineCap:"round",strokeWidth:b/6,originX:"center",originY:"center"}),i.add(h)}),i.on("mouse:move",function(a){if(l){var b=i.getPointer(a.e);h.set({x2:b.x,y2:b.y}),i.renderAll()}}),i.on("mouse:up",function(c){l=!1;var e=i.getPointer(c.e),f=[e.x,e.y];if(k=[Math.floor(f[1]/b),Math.floor(f[0]/b)],i.remove(h),j[0]!==k[0]||j[1]!==k[1]){var g=n();if(-1!==g){var m=d[g];m=j.reverse().join("").concat(k.reverse().join("")).split(""),m=m.map(function(a){return a*b+b/2});var p=new a.Line(m,{stroke:"black",strokeLineCap:"round",strokeWidth:b/6,originX:"center",originY:"center",selectable:!1});o(d[g]),d.splice(g,1),i.add(p),0===d.length&&r()}}j=[],k=[]});var r=function(){};q()}(fabric,SIZE,GRID,words);