2015-09-04 69 views
1

我的意图是通过选择字体类型来改变字体,我将如何通过选择即时更改字体。添加后的面料画布字体样式更改

我改变字体代码:

//the font wont change getAcriveObject 
    $(function() { 
     $('#FontStyleNumber').change(function(){ 
     var cFont = $(this).val(); 
     var tObj = canvas.getActiveObject(); 
     tObj.set({fontFamily : cFont}); 
     canvas.renderAll(); 
     }); 
    }); 

的jsfiddle: mylink

回答

3

有,因为你应该包括jQuery的onload事件里面的所有脚本:

Your fixed example

尝试自己在下面的代码片段,如果解决方案适合你:

$(function() { 
 
    var canvas = new fabric.Canvas('textCanvas'); 
 

 
    var textObj1 = new fabric.Text('font test', { 
 
    left: 100, 
 
    top: 100, 
 
    fontSize: 30, 
 
    fill: "#FF0000" // Set text color to red 
 
    }); 
 

 
    canvas.add(textObj1); 
 
    $('#FontStyleNumber').change(function() { 
 
    
 
    var mFont = $(this).val(); 
 
    var tObj = canvas.getActiveObject(); 
 
    tObj.set({ 
 
     fontFamily: mFont 
 
    }); 
 
    canvas.renderAll(); 
 
    }); 
 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<label>Select Font:</label> 
 
<select name="FontStyleNumber" id="FontStyleNumber"> 
 
    <option value="Times New Roman">Times New Roman</option> 
 
    <option value="Arial">Arial</option> 
 
    <option value="Georgia">Georgia</option> 
 
    <option value="Hammersmith One">Hammersmith One</option> 
 
    <option value="Pacifico">Pacifico</option> 
 
    <option value="Anton">Anton</option> 
 
    <option value="Sigmar One">Sigmar One</option> 
 
    <option value="Righteous">Righteous</option> 
 
</select> 
 
<div class="textcanvas_warpper"> 
 
    <canvas id="textCanvas" width="500" height="500" style="border: 1px solid black; margin-left:50px; margin-top:35px; z-index:1;"></canvas> 
 
</div>