javascript - FabricJS CurvedText object (extended) is not getting displayed on updating spacing and text properties -
i have updated fabric curvedtext extension because of our requirement different. want set auto spacing between characters covers full circle. got solutions after updating code in fabric curvedtext extension. while changing text of object (on keyup event of textbox), recalculate spacing , setting spacing , text via settext , set("spacing", val) properties. in situations, i can not see object on canvas i can see pointer of object on canvas mouse hover effect.
remove characters textbox in snippet , object hide after remove characters. same thing happens when add characters in textbox.
updated curved text extension.
var radius = 100; var circum = math.floor(2 * math.pi * radius); var spacing = 0; var fontfamily = 'arial'; var fontsize = 30; var text; var lineheight = 1; var topa = 100; var left = 200; var char = 0; var textwidth = 0; var obj; var canvas, ctx; $(document).ready(function () { canvas = new fabric.canvas('c'); ctx = canvas.getcontext("2d"); text = $('#uppertext').val(); spacingoperation(); var uppertext = new fabric.curvedtext(text, { radius: radius, top: topa, left: left, fontfamily: fontfamily, fontsize: fontsize, lineheight: lineheight, spacing: spacing, originx: 'center', hascontrols: true, hasborders: true, selectable: true, textalign: 'center' }); canvas.add(uppertext).renderall(); $('#uppertext').on('keyup', function () { text = $('#uppertext').val(); setobjects(); if (spacingoperation()) { setprops(); canvas.renderall(); } }); }); function spacingoperation() { textwidth = gettotaltextwidth(); spacing = 0; textwidth = gettotaltextwidth(); var diff = math.ceil(circum - textwidth); spacing = math.ceil(diff / char); return true; } function gettotaltextwidth() { ctx.font = fontsize + 'px ' + fontfamily; char = text.length; txtwidth = math.ceil(ctx.measuretext(text).width + (spacing * char)); return txtwidth; } function setobjects() { canvas.setactiveobject(canvas.item(0)); obj = canvas.getactiveobject(); } function setprops() { obj.set('spacing', spacing); obj.settext(text); obj.setcoords(); }
<!doctype html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.js"></script> <script src="http://www.trimantra.com/demo/fabric.curvedtext_done.js"></script> </head> <body> <div class="row"> <div class="row canvas-container" style="width: 50%; float: left;"> <canvas id="c" width="400" height="400" style="border:1px dotted #000;"></canvas> </div> <div class="row" style="float: left; width: 50%; height: 150px;"> upper text : <input type="text" id="uppertext" value="utxxxxxxxxxxxxxutxxxxxaaaaaaaawwwwwwwwwxxxxxxxxutxxxxxxxxxxxxxutxxxxxxxxxxxxx" /> </div> </div> </body> </html>
i have looked code , changed 1 line in fabric curvedtext extension ctx = new fabric.canvas('c').getcontext('2d');
ctx = fabric.util.createcanvaselement().getcontext('2d');
, working fine.
Comments
Post a Comment