10

I'm looking forward to creating a very simple way that allows users to write, resize, position or change the color of a text inside a <div>. I know jQuery little bit.

My HTML

<div class="canvas">
    <div id ="u-test-id" class="u-test-class"  contenteditable="true">
        Testing
    </div>
</div>

Here .canvas is a <div>, sometimes it contains an image or another background color. I already write the code to change the color and background image of that <div class="canvas">. What I need is that the user can enter the text very easily and he can style it as he wishes. Please see the image:

enter image description here

This image i capure from mailchimp. In mailchimp when we creating an template design , it will provide the same thing i wanted . Here user can add text , he can rotate text , change font family, color etc .

Actually the color and font family things , i know how to do this with jquery .But the rotation of the text div is complicated i think. How to do this rotation ?

Here I am not asking for code, but I want to see a working demo that helps me to understand the working and learn jQuery. Please anyone can give the sample for this work, or suggest any free jQuery plugin or the basic code.

  • https://jqueryui.com/resizable/ – prasanth Oct 18 '16 at 08:34
  • Thank you prasad for the quick response . I already aware of this function. But in my case resize() is not enough . –  Oct 18 '16 at 08:40
  • I think You need like a XL sheet input box – prasanth Oct 18 '16 at 08:43
  • no. Please see this http://www.jqueryrain.com/?fjpPW0bb . here image , but i need text instead of image –  Oct 18 '16 at 08:52
  • apply the -- contenteditable="true" -- attribute to the parent DIV, Most modern browsers come with built in editor tools... – Hudson Oct 18 '16 at 08:56
  • I think this is cropping tool, It is useful for image but cropping text how that is possible ? – Sunil Kumar Oct 18 '16 at 09:32
  • it is like edit text option in windows paint or like in photoshop software –  Oct 18 '16 at 09:44
  • Will adding a rich jQuery text editor do the job for you? for example http://jqueryte.com/demos . There are many such editors. – Mark Wilson Oct 18 '16 at 12:41
  • Please read my updated question –  Oct 24 '16 at 13:00
  • I think easiest way is to use textarea editable, also using jqueryrotate library will do the job, take a look at my simple example at answer @Ron Then you can place over, the buttons floating to add functionalities. – Jordi Flores Oct 26 '16 at 10:25
  • Can this plnkr help: http://plnkr.co/edit/xaCWl9Havv1ow7bhMTf2?p=preview ? – arnabkaycee Oct 27 '16 at 11:22

3 Answers3

4

I found this from jsfiddle. Lets take this scratch and start extending your functionality

HTML

<div class='resizable draggable'>
    <h1>Resize Me</h1>
    <div class="ui-resizable-handle ui-resizable-nw"></div>
    <div class="ui-resizable-handle ui-resizable-ne"></div>
    <div class="ui-resizable-handle ui-resizable-sw"></div>
    <div class="ui-resizable-handle ui-resizable-se"></div>
    <div class="ui-resizable-handle ui-resizable-n"></div>
    <div class="ui-resizable-handle ui-resizable-s"></div>
    <div class="ui-resizable-handle ui-resizable-e"></div>
    <div class="ui-resizable-handle ui-resizable-w"></div>
</div>

Javascript:

 $('.resizable').resizable({
        handles: {
            'nw': '.ui-resizable-nw',
            'ne': '.ui-resizable-ne',
            'sw': '.ui-resizable-sw',
            'se': '.ui-resizable-se',
            'n': '.ui-resizable-n',
            'e': '.ui-resizable-e',
            's': '.ui-resizable-s',
            'w': '.ui-resizable-w'
        }
    });
    $( '.draggable' ).draggable().on('click', function(){
        if ( $(this).is('.ui-draggable-dragging') ) {
            return;
        } else {
            $(this).draggable( 'option', 'disabled', true );
            $(this).prop('contenteditable','true');
            $(this).css('cursor', 'text');
        }
    })
    .on('blur', function(){
        $(this).draggable( 'option', 'disabled', false);
        $(this).prop('contenteditable','false');
        $(this).css('cursor', 'move');
    });

CSS:

.draggable {
    cursor: move;
}
.resizable {
    border: 1px dashed #000000;
    position: relative;
    min-height: 50px;
}
.ui-resizable-handle {
    width: 10px;
    height: 10px;
    background-color: #ffffff;
    border: 1px solid #000000;
    position: absolute;
}
.ui-resizable-nw {
    left: -5px;
    top: -5px;
    cursor: nw-resize;
}
.ui-resizable-ne {
    top: -5px;
    right: -5px;
    cursor: ne-resize;
}
.ui-resizable-sw {
    bottom: -5px;
    left: -5px;
    cursor: sw-resize;
}
.ui-resizable-se {
    bottom: -5px;
    right:-5px;
    cursor: se-resize;
}
.ui-resizable-n {
    top: -5px;
    left:50%;
    cursor: n-resize;
}
.ui-resizable-s {
    bottom: -5px;
    left: 50%;
    cursor: s-resize;
}
.ui-resizable-w {
    left:-5px;
    top:calc(50% - 5px);
    cursor: w-resize;
}
.ui-resizable-e {
    right:-5px;
    top:calc(50% - 5px);
    cursor: e-resize;
}
mymotherland
  • 7,968
  • 14
  • 65
  • 122
  • Please see my updated question . Please go through http://mailchimp.com/ . –  Oct 24 '16 at 12:55
1

what you need is http://jqueryrotate.com/ in conjuction with other functions.

This example should be improved, but you have this functionalities:

  1. Rotate
  2. Close
  3. Edit
  4. Dragg
  5. Resize (textarea)

Some notes about this example.

-Rotate is done by the jqueryrotate.com library. in this example I'm using taking 0,0 position to calculate angle, but should be improved to take angle towards the textarea to a better user experience.

-Resize & edit functions are supported by textarea editable, that will do quite well the job, as when resizing, it automatically resizes its parent wrapper, no need for extra code here.

-Drag is supported by jquery.ui , (perhaps it will be usefull to disable drag when doing rotation, in that case just need to add a flag) .

//initial rotation, just for test
//$(".wrapper").rotate(-45);


//let's add draggable functionality
$( function() {
    $( ".wrapper" ).draggable();
  } );

 //close button action
 document.getElementById('close').onclick = function() {
        this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
        return false;
};

//rotate button action
var rotate = $('#rotate')
var dragging = false;
rotate.mousedown(function() {
    dragging = true
})
$(document).mouseup(function() {
    dragging = false
})
$(document).mousemove(function(e) {
    if (dragging) {

        var mouse_x = e.pageX / 2;
        var mouse_y = e.pageY / 2;
        var radians = Math.atan2(mouse_x - 10, mouse_y - 10);
        var degree = (radians * (180 / Math.PI) * -1) + 90;
      
        $(".wrapper").rotate(degree);
      }
  })
.wrapper {
  display: inline-block;
  position:relative;
  border: dotted 1px #ccc;
}
#close {
    float:left;
    display:inline-block;
    padding:2px 5px;
    background:#ccc;
}
#rotate {
  position: absolute;
  display: inline-block;
  background-image:url(https://www.iconexperience.com/_img/o_collection_png/green_dark_grey/512x512/plain/rotate_right.png);
  background-repeat: no-repeat; 
  background-size: contain;
  width: 20px;
  height: 20px;
  bottom: -10px;
  right: -10px;

}
textarea {
  margin:15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script
  src="http://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
  integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
  crossorigin="anonymous"></script>

<script src="http://cdn.sobekrepository.org/includes/jquery-rotate/2.2/jquery-rotate.min.js"></script>


<div class="wrapper">
  <span id="close">x</span>
  <span id="rotate"></span>
<textarea contenteditable="true" id="editable">this is a textarea that i will rotate</textarea>
  
</div>
Jordi Flores
  • 2,080
  • 10
  • 16
  • Thank you for this code . But text resizing not working . Also could you please show a working demo in jsfiddle withe image and editable text . –  Oct 27 '16 at 09:50
  • image? you requested for a text tool. I'll try to make the resize text function – Jordi Flores Oct 27 '16 at 10:54
  • on this jsfiddle I've made an resizable & editable text. https://jsfiddle.net/w5zfntz1/ – Jordi Flores Oct 27 '16 at 12:47
0

To rotate div, you can do it in mulitple ways. Ideally it should be implemented like the one which is explained in the below link which will support in all the browsers. How to rotate a div using jQuery

Hope this helps!!!

Community
  • 1
  • 1