cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="wrapper">
    
    <div class="controls controls--units">
        <h2 class="controls__title">maskContentUnits:</h2>
        <button class="button button--maskUnits" 
                value="userSpaceOnUse">userSpaceOnUse</button>
        <button class="button button--maskUnits"
                value="objectBoundingBox">objectBoundingBox</button>
    </div>
    
    <main class="main">
        
        <div class="controls controls--ranges">
            <div class="range range--hor">
                <div class="range__title range--hor__title"></div>
                <input type="range" 
                       class="range__input" id="range--hor__input" 
                       min="0" max="100" step="1"/>
            </div>

            <div class="range range--vert">
                <div class="range__title range--vert__title"></div>
                <input type="range" 
                       class="range__input" id="range--vert__input" 
                       min="0" max="100" step="1"/>
            </div>
        </div>

        <svg class="svg">

            <symbol id="s-mask">
                <rect 
                      width="100%" height="100%"
                      fill="#555"></rect>
                    <circle r="30%" cx="50%" cy="50%"
                            stroke="gray" 
                            stroke-width="3%" 
                            fill="white"
                            class="mask__circle"></circle>
            </symbol>

            <mask id="mask" 
                  x="0" y="0">
                <use xlink:href="#s-mask"></use>
            </mask>

            <pattern id="pattern"
                      viewBox="0 0 470 400"
              width="470" height="400" patternUnits="userSpaceOnUse">
                <path d="M77,0 L234,0 L155.5,400 L77,0 Z" fill="#FF9F00"></path>
                    <polygon fill="#EE2A42" points="77.5 0 156 400 -1 400 "></polygon>
                    <path d="M-79,0 L78,0 L-0.5,400 L-79,0 Z" fill="#A506CD"></path>
                    <path d="M233,0 L390,0 L311.5,400 L233,0 Z" fill="#7ED321"></path>
                    <polygon fill="#F8E81C" points="233.5 0 312 400 155 400 "></polygon>
                    <polygon fill="#049BD8" points="389.5 0 468 400 311 400 "></polygon>
                    <path d="M389,0 L546,0 L467.5,400 L389,0 Z" fill="#A506CD"></path>
            </pattern>

            <symbol id="s-coords">
                <g stroke-dasharray="10 5">
                    <line x1="50%" y1="0" x2="50%" y2="100%" />
                    <line y1="50%" x1="0" y2="50%" x2="100%" />
                </g>
            </symbol>

            <use xlink:href="#s-coords" stroke="#333"></use>

            <g>
                <rect width="80%" height="90%"
                      fill="steelblue" fill-opacity=".3"
                      stroke="steelblue" stroke-width="3"></rect>

                <rect mask="url(#mask)"
                      width="80%" height="90%"
                      fill="url(#pattern)"></rect>

                <use xlink:href="#s-coords" width="80%" height="90%" stroke="steelblue"></use>
            </g>

        </svg>
    </main>
    
</div>
            
          
!
            
              $svgW: 500px;
$svgH: 300px;

$controlH: 40px;

@mixin valnMid {
    &:before {
            content: '';
            display: inline-block;
            height: 100%;
            vertical-align: middle;
            }
}

HTML, BODY {
    height: 100%;
    }

BODY {
    background: 
        linear-gradient( #EEE 1px, transparent 2px),
        linear-gradient( to right, #EEE 1px, transparent 2px);
    background-size: 1em 1em;
    }

// Layout 

.wrapper {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
    width: $svgW + $controlH;
    height: $svgH + $controlH * 2.5;
    text-align: center;
    white-space: nowrap;
    font-family: Arial, sans-serif;
    }

.main {
    position: relative;
    height: $svgH + $controlH; 

    &:after {
        content: '';
        display: table;
        clear: both;
        }
    }

.svg {
    position: absolute;
    right: 0;
    bottom: 0;
    width: $svgW; 
    height: $svgH;
    box-sizing: border-box;
    border: 2px solid #333;
    }   

// Controls

.controls {
    height: $controlH;
    &__title {
        display: inline-block;
        font-weight: bold;
        }
    }

    .button {
        padding: .3rem .5rem;
        border: 0;
        background: #BBB;
        cursor: pointer;
        font-size: 1rem;
        text-shadow: 0 0 1px rgba(0, 0, 0, .75);
        color: #FFF;
        }

    .controls--units {
        margin-bottom: $controlH / 2;
        font-size: 1rem;
        
        @include valnMid;
        }
        .controls--units .button {
            margin-left: .5rem;
            
            &--current {
               background: crimson; 
                }
            }
    
        .controls--showmask {
            @include valnMid;
            text-align: right;
            }
            .button--showmask {
                &--active {
                   background: steelblue; 
                   }
                }

    .controls--ranges {
        position: absolute;
        width: 100%;
        }
        .range {
            position: absolute;
            text-align: center;

            &--hor {
                right: 0;
                width: $svgW;
                }

            &--vert {
                top: 40px;
                width: $svgH;
                transform-origin: 0 0;
                transform: rotate(-90deg) translateX(-100%);
                }

                &--vert .range__input {
                    transform: rotate(180deg);
                    }

            input {
                width: 100%;
                margin: auto;
                cursor: pointer;
                }
            }

.showmask {
    display: none;
    
    &--visible {
        display: block;
        }
    }
            
          
!
            
              var rangeHor = doc.getElem('#range--hor__input');
var rangeHorTitle = doc.getElem('.range--hor__title');

var rangeVert = doc.getElem('#range--vert__input');
var rangeVertTitle = doc.getElem('.range--vert__title');

var rangesList = {};

var mask = doc.getElem('#mask');
var maskCircle = doc.getElem('.mask__circle');

var maskCircleObj = {
    elem: maskCircle,
    attrs: ['r', 'cx', 'cy', 'stroke-width'],
    isPercents: true
};

var unitTypes = {
    1: 'fractions',
    2: 'percents'
};

var buttons = doc.getElemsList('.button--maskUnits');
var buttonsProp = 'maskContentUnits';
var currentButton;
var currentButtonClass = 'button--current';

var inputMarks = [
    {
        id: 'hor',
        valName: 'cx'
    }, 
    {
        id: 'vert',
        valName: 'cy'
    }
];

//------------------------------

function Range ( obj ) {
    this.elem = doc.getElem( obj.rangeInputId );
    this.title = doc.getElem( obj.titleClass );
    this.valName = obj.valName;
    this.attrs = ['step', 'max', 'value'];
    this.isPercents = false;
    
    var that = this;
    
    this.elem.oninput = function () {
        that.setTitleText();
    };
};

//------------------------------

Range.prototype.setTitleText = function () {
    var value = this.elem.value;
    var valueWithUnits = value;
    var maskCircleProp = {};
    
    if ( currentButton.value ===  'userSpaceOnUse') {
        valueWithUnits = value + '%';
    }
    
    maskCircleProp[ this.valName ] = valueWithUnits;
    maskCircle.setAttr(maskCircleProp);
    
    this.title.innerHTML = this.valName + ': ' + valueWithUnits;
};

//------------------------------

function Button( elem ) {
    
    this.elem = elem;
    
    if ( !currentButton ) {
        currentButton = this.elem;
        this.elem.classList.add(currentButtonClass);
    }
    
    this.elem.onclick = function () {
        if (currentButton === this ) {
            return;
        }
        
        if ( currentButton ) {
            currentButton.classList.remove(currentButtonClass);
        }
        this.classList.add(currentButtonClass);
        var maskProp = {};
    maskProp[ buttonsProp ] = elem.value;
        mask.setAttr( maskProp );
        currentButton = elem;

        changeUnits();
    }
}

//------------------------------

function convertVal( attrVal ) {
    var attrNewVal = 0;
    
    //objectBoundingBox
    if ( currentButton.value ===  'objectBoundingBox') {
            
        if ( attrVal < 1 ) {
            attrNewVal = attrVal;
        }
        else {
            attrNewVal = attrVal / 100;
        }    
    }
    //userSpaceOnUse
    else {
        if ( attrVal <= 1 ) {
            attrNewVal = attrVal * 100; 
        }
        else {
            attrNewVal = attrVal;
        }
    }
    
    return attrNewVal;
}

//------------------------------

function setNewAttrs( item ) {
    
    var elem = item.elem;
    var attrs = item.attrs;
    var isPercents = item.isPercents;
    var elemValue = elem.value;
    var elemNewValue = '';
    var isSVG = elem.farthestViewportElement ? true : false;
    
    if ( !attrs ) {
        return;
    }
    
    for ( var k = 0; k < attrs.length; k++ ) {
        var attrsNewVals = {};
        var attrName = attrs[ k ];
        
        var attrVal = elem.getAttr( attrName ).replace('%','');  
        
        // Prevent change value by browser 
        // if step was changed before
        if ( attrName === 'value' && attrVal != elemValue ) {
            attrVal = elemValue;
        }
        
        var attrNewVal = convertVal( attrVal );
        
        if ( isSVG && currentButton.value === 'userSpaceOnUse' ) {
            attrNewVal += '%';    
        }
        
        attrsNewVals[ attrName ] = attrNewVal;
        
        // Keep particular order to keep properties
        elem.setAttr( attrsNewVals );
        
        // Keep new value for checking result
        if ( attrName === 'value' ) {
            elemNewValue = attrNewVal;
        }
    }
    
    // If browser lost original value
    if ( elem.value !== elemNewValue ) {
        elem.value = elemNewValue;
    }
    
    if ( item.setTitleText ) {
        item.setTitleText();
    }
}

//------------------------------

function changeUnits() {
    var unitsList = [
        rangesList.cx,
        rangesList.cy,
        maskCircleObj
    ];
    
    for ( var i = 0; i < unitsList.length; i++ ) {
        setNewAttrs( unitsList[i] );    
    }
    
}

//------------------------------
// Init Buttons

for ( var i = 0; i < buttons.length; i ++ ) {
    var button = new Button( buttons[i] );
}

//------------------------------
// Init Ranges 

for ( var i = 0; i < inputMarks.length; i++ ) {
    
    var markItem = inputMarks[i];
    var rangeData = {
        rangeInputId: '#range--' + markItem.id + '__input',
        titleClass: '.range--' + markItem.id + '__title',
        valName: markItem.valName
    };
    
    var range = new Range( rangeData );
    range.setTitleText();
    rangesList[ markItem.valName ] = range;
}

//------------------------------

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console